Re: Python string replace the values

2017-09-02 Thread Peter Otten
Steve D'Aprano wrote: > On Sat, 2 Sep 2017 03:13 am, Ganesh Pal wrote: > >> Example : >> >> "a" and "1" => a0001 >> >> "a" and "aa" => c00aa > > Why does the leading 'a' change to a 'c'? Is that a mistake? I'll assume > its a typo. > > You want string slicing. > > base = 'a'

Re: Python string replace the values

2017-09-02 Thread Ganesh Pal
MRAB , Thanks for your solution it looks neat and best ! On Fri, Sep 1, 2017 at 11:14 PM, MRAB wrote: > On 2017-09-01 18:13, Ganesh Pal wrote: > >> In the fixed length string i.e "a",last 4 bits i.e "" should be >> replaced by the user provided value ( the value is between 0001 + f f f

Re: Python string replace the values

2017-09-01 Thread Steve D'Aprano
On Sat, 2 Sep 2017 03:13 am, Ganesh Pal wrote: > Example : > > "a" and "1" => a0001 > > "a" and "aa" => c00aa Why does the leading 'a' change to a 'c'? Is that a mistake? I'll assume its a typo. You want string slicing. base = 'a' extra = 'ab' print( base[:-len(extra)] + extra

Re: Python string replace the values

2017-09-01 Thread Ganesh Pal
>Note you should think VERY CAREFULLY about any user input like this. What are you going to do the user gives you too many digits, too few digits, non-digits Thanks the solution i.e using zfill() looks simple :) , we have a check that will take care of user input than's for noticing that On Fri,

Re: Python string replace the values

2017-09-01 Thread Ganesh Pal
> (I assume that in your example "a" and "aa" => c00aa, that really should have been a00aa) Yes , thanks for noticing. On Fri, Sep 1, 2017 at 11:18 PM, Irv Kalb wrote: > > > On Sep 1, 2017, at 10:13 AM, Ganesh Pal wrote: > > > > In the fixed length string i.e "a",last 4 bits i.e "00

Re: Python string replace the values

2017-09-01 Thread Irv Kalb
> On Sep 1, 2017, at 10:13 AM, Ganesh Pal wrote: > > In the fixed length string i.e "a",last 4 bits i.e "" should be > replaced by the user provided value ( the value is between 0001 + f f f f ) > . I intend to form final_string using a fixed_string and an user provided > user_string >

Re: Python string replace the values

2017-09-01 Thread Rhodri James
On 01/09/17 18:13, Ganesh Pal wrote: "a" + "1"===> expected was a0001 'a1' Why would you expect that? Concatenation means "joining together", so >>> "foo" + "bar" 'foobar' No other mangling of either of your original strings is going to happen; there is no magic going on here.

Re: Python string replace the values

2017-09-01 Thread MRAB
On 2017-09-01 18:13, Ganesh Pal wrote: In the fixed length string i.e "a",last 4 bits i.e "" should be replaced by the user provided value ( the value is between 0001 + f f f f ) . I intend to form final_string using a fixed_string and an user provided user_string Example: fixed_st

Python string replace the values

2017-09-01 Thread Ganesh Pal
In the fixed length string i.e "a",last 4 bits i.e "" should be replaced by the user provided value ( the value is between 0001 + f f f f ) . I intend to form final_string using a fixed_string and an user provided user_string Example: fixed_string = "a" user_string ='1' final str

Issue 16061 performance regression in string replace for 3.3

2013-04-13 Thread Mark Lawrence
Fixed full details here http://bugs.python.org/issue16061 -- If you're using GoogleCrap™ please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence -- http://mail.python.org/mailman/listinfo/python-list

Re: string replace for back slash

2009-02-05 Thread S.Selvam Siva
On Thu, Feb 5, 2009 at 5:59 PM, wrote: > "S.Selvam Siva" wrote: > > I tried to do a string replace as follows, > > > > >>> s="hi & people" > > >>> s.replace("&","\&") > > 'hi \\& peo

string replace for back slash

2009-02-05 Thread rdmurray
"S.Selvam Siva" wrote: > I tried to do a string replace as follows, > > >>> s="hi & people" > >>> s.replace("&","\&") > 'hi \\& people' > >>> > > but i was expecting 'hi \&a

Re: string replace for back slash

2009-02-05 Thread Chris Rebert
On Thu, Feb 5, 2009 at 3:40 AM, S.Selvam Siva wrote: > Hi all, > > I tried to do a string replace as follows, > >>>> s="hi & people" >>>> s.replace("&","\&") > 'hi \\& people' >>>> > &

string replace for back slash

2009-02-05 Thread S.Selvam Siva
Hi all, I tried to do a string replace as follows, >>> s="hi & people" >>> s.replace("&","\&") 'hi \\& people' >>> but i was expecting 'hi \& people'.I dont know ,what is something different here with escape sequence. -- Yours, S.Selvam -- http://mail.python.org/mailman/listinfo/python-list

Re: alternating string replace

2008-01-11 Thread Paddy
On Jan 11, 8:55 pm, "Reedick, Andrew" <[EMAIL PROTECTED]> wrote: > > -Original Message- > > From: [EMAIL PROTECTED] [mailto:python- > > [EMAIL PROTECTED] On Behalf Of cesco > > Sent: Wednesday, January 09, 2008 5:34 AM > > To: [EMAIL PROT

Re: alternating string replace

2008-01-11 Thread Paddy
On Jan 12, 2:55 am, Pablo Ziliani <[EMAIL PROTECTED]> wrote: > * die, thread! :-) def altrep7(s): from itertools import cycle import re a = cycle(':,') return re.sub('_', lambda x: a.next(), s) altrep7.author="George Sakkis(&Paul Rubin)" Gives: ## Program by: George Sakkis(&Paul Rubi

Re: alternating string replace

2008-01-11 Thread Pablo Ziliani
Paul Rubin wrote: > George Sakkis <[EMAIL PROTECTED]> writes: > >> from itertools import chain, izip, cycle >> print ''.join(chain(*izip(s1.split('_'),cycle(':,'[:-1] >> > > from itertools import cycle > a = cycle(':,') > print re.sub('_', lambda x: a.next(), s1) > Lovely. If there

Re: alternating string replace

2008-01-11 Thread Steven D'Aprano
On Fri, 11 Jan 2008 14:55:18 -0600, Reedick, Andrew wrote: > For those of us who still think in Perl, here's an easy to read ... > s = re.sub(r'_(.*?(_|$))', r':\1', s) "Easy to read"? Oh that's priceless. Andrew, you should consider writing comedy professionally! -- Steven -- http://mail.

Re: alternating string replace

2008-01-11 Thread Paul Rubin
George Sakkis <[EMAIL PROTECTED]> writes: > from itertools import chain, izip, cycle > print ''.join(chain(*izip(s1.split('_'),cycle(':,'[:-1] from itertools import cycle a = cycle(':,') print re.sub('_', lambda x: a.next(), s1) -- http://mail.python.org/mailman/listinfo/python-list

Re: alternating string replace

2008-01-11 Thread George Sakkis
On Jan 9, 6:34 am, Duncan Booth <[EMAIL PROTECTED]> wrote: > cesco <[EMAIL PROTECTED]> wrote: > > Hi, > > > say I have a string like the following: > > s1 = 'hi_cat_bye_dog' > > and I want to replace the even '_' with ':' and the odd '_' with ',' > > so that I get a new string like the following: >

RE: alternating string replace

2008-01-11 Thread Reedick, Andrew
> -Original Message- > From: [EMAIL PROTECTED] [mailto:python- > [EMAIL PROTECTED] On Behalf Of cesco > Sent: Wednesday, January 09, 2008 5:34 AM > To: python-list@python.org > Subject: alternating string replace > > Hi, > > say I have a string like the fol

Re: alternating string replace

2008-01-11 Thread Paddy
On Jan 11, 9:54 am, Chris <[EMAIL PROTECTED]> wrote: > On Jan 9, 12:34 pm, cesco <[EMAIL PROTECTED]> wrote: > > > Hi, > > > say I have a string like the following: > > s1 = 'hi_cat_bye_dog' > > and I want to replace the even '_' with ':' and the odd '_' with ',' > > so that I get a new string like

Re: alternating string replace

2008-01-11 Thread Paddy
On Jan 11, 9:31 am, [EMAIL PROTECTED] wrote: > evenOrOdd = True > s1, s2 = "hi_cat_bye_dog_foo_bar_red", "" > > for i in s1: >if i == '_': >s2 += ':' if evenOrOdd else ',' >evenOrOdd = not evenOrOdd >else: >s2 += i > > print s2 > > Presently I cannot work out how to

Re: alternating string replace

2008-01-11 Thread bearophileHUGS
Dennis Lee Bieber: > So� in Python, your str[n] := > ':' just can not be done! You would have to create a new string > containing everything in front of n, the ':', and then everything behind > n (skipping n itself, of course). This is a painfully slow operation in > Python as it allocates memory f

Re: alternating string replace

2008-01-11 Thread Chris
On Jan 9, 12:34 pm, cesco <[EMAIL PROTECTED]> wrote: > Hi, > > say I have a string like the following: > s1 = 'hi_cat_bye_dog' > and I want to replace the even '_' with ':' and the odd '_' with ',' > so that I get a new string like the following: > s2 = 'hi:cat,bye:dog' > Is there a common recipe t

Re: alternating string replace

2008-01-11 Thread cokofreedom
evenOrOdd = True s1, s2 = "hi_cat_bye_dog_foo_bar_red", "" for i in s1: if i == '_': s2 += ':' if evenOrOdd else ',' evenOrOdd = not evenOrOdd else: s2 += i print s2 Presently I cannot work out how to use .join instead of += ... While I realise this is producing a new

Re: alternating string replace

2008-01-10 Thread cokofreedom
On Jan 10, 3:46 am, [EMAIL PROTECTED] wrote: > Gordon C: > > > This is very cool stuff but I suspect that the code is unreadable > > to many readers, including me. Just for fun here is a complete program, > > written in Turbo Pascal, circa 1982, that does the job. Readable > > n'est pas? > > I thi

Re: alternating string replace

2008-01-09 Thread bearophileHUGS
Gordon C: > This is very cool stuff but I suspect that the code is unreadable > to many readers, including me. Just for fun here is a complete program, > written in Turbo Pascal, circa 1982, that does the job. Readable > n'est pas? I think it's quite readable, especially if you indent it more cor

Re: alternating string replace

2008-01-09 Thread Gordon C
This is very cool stuff but I suspect that the code is unreadable to many readers, including me. Just for fun here is a complete program, written in Turbo Pascal, circa 1982, that does the job. Readable n'est pas? Program dash; var str: string[80]; n: integer; odd: boolean; begin

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Paddy
On Jan 9, 9:39 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Paddy wrote: > > To see how they act against 'corner cases' and > > an exercise for me in trying to create corner cases. (I'm in to > > functional testing at the mo'). > > sounds more like "pulling requirements out of thin air". not sur

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Paddy
On Jan 9, 9:29 pm, Paddy <[EMAIL PROTECTED]> wrote: > On Jan 9, 8:56 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > > > Donald 'Paddy' McCarthy wrote: > > > I created some more test strings and ran posters solutions against them. > > > the point being? > > > > > To see how they act against 'corner

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Fredrik Lundh
Paddy wrote: > To see how they act against 'corner cases' and > an exercise for me in trying to create corner cases. (I'm in to > functional testing at the mo'). sounds more like "pulling requirements out of thin air". not sure that helps the OP get a better understanding of Python, really.

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Paddy
On Jan 9, 8:56 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Donald 'Paddy' McCarthy wrote: > > I created some more test strings and ran posters solutions against them. > > the point being? > > To see how they act against 'corner cases' and an exercise for me in trying to create corner cases. (I

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Paddy
On Jan 9, 8:47 pm, [EMAIL PROTECTED] wrote: > Donald 'Paddy' McCarthy: > > [... lots and lots and lots of tests...] > > C'mon Paddy, where are the timings too? Are you becoming lazy > lately? ;-) > > Bear bugs, > bearophile Get it right before you get it fast. But what is 'right'. -- http://mail.

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Fredrik Lundh
Donald 'Paddy' McCarthy wrote: > I created some more test strings and ran posters solutions against them. the point being? -- http://mail.python.org/mailman/listinfo/python-list

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread bearophileHUGS
Donald 'Paddy' McCarthy: [... lots and lots and lots of tests...] C'mon Paddy, where are the timings too? Are you becoming lazy lately? ;-) Bear bugs, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: alternating string replace

2008-01-09 Thread Paul McGuire
On Jan 9, 7:41 am, "Neil Cerutti" <[EMAIL PROTECTED]> wrote: > On Jan 9, 2008 5:34 AM, cesco <[EMAIL PROTECTED]> wrote: > > > Hi, > > > say I have a string like the following: > > s1 = 'hi_cat_bye_dog' > > and I want to replace the even '_' with ':' and the odd '_' with ',' > > so that I get a new

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Donald 'Paddy' McCarthy
cesco wrote: I created some more test strings and ran posters solutions against them. results attached. - Paddy. # alternating_replacements.py tests = " 1 2_ 3_4 5_6_ 7_8_9 10_11_12_ 13_14_15_16 17_18_19_20_" \ " _ _21 _22_ _23_24 _25_26_ _27_28_29 _30_31_32_ _33_34_35_36" \ " __ _

Re: alternating string replace

2008-01-09 Thread Pablo Ziliani
cesco wrote: > Hi, > > say I have a string like the following: > s1 = 'hi_cat_bye_dog' > and I want to replace the even '_' with ':' and the odd '_' with ',' > so that I get a new string like the following: > s2 = 'hi:cat,bye:dog' > Is there a common recipe to accomplish that? I can't come up with

Re: alternating string replace

2008-01-09 Thread Neil Cerutti
On Jan 9, 2008 5:34 AM, cesco <[EMAIL PROTECTED]> wrote: > Hi, > > say I have a string like the following: > s1 = 'hi_cat_bye_dog' > and I want to replace the even '_' with ':' and the odd '_' with ',' > so that I get a new string like the following: > s2 = 'hi:cat,bye:dog' > Is there a common reci

Re: alternating string replace

2008-01-09 Thread Neil Cerutti
On Jan 9, 2008 5:34 AM, cesco <[EMAIL PROTECTED]> wrote: > Hi, > > say I have a string like the following: > s1 = 'hi_cat_bye_dog' > and I want to replace the even '_' with ':' and the odd '_' with ',' > so that I get a new string like the following: > s2 = 'hi:cat,bye:dog' > Is there a common reci

Re: alternating string replace

2008-01-09 Thread cokofreedom
Designed a pretty basic way that is "acceptable" on small strings. evenOrOdd = True s1 = "hi_cat_bye_dog_foo_bar_red" s2 = "" for i in s1: if i == '_': if evenOrOdd: s2 += ':' evenOrOdd = not evenOrOdd else: s2 += ',' evenOrOdd

Re: alternating string replace

2008-01-09 Thread bearophileHUGS
My version, uses a re.sub, plus a function used as an object with a one bit state: from re import sub def repl(o): repl.n = not repl.n return ":" if repl.n else "," repl.n = False print sub("_", repl, "hi_cat_bye_dog_foo_bar_red") Bye, bearophile -- http://mail.python.org/mailman/listi

Re: alternating string replace

2008-01-09 Thread Duncan Booth
cesco <[EMAIL PROTECTED]> wrote: > Hi, > > say I have a string like the following: > s1 = 'hi_cat_bye_dog' > and I want to replace the even '_' with ':' and the odd '_' with ',' > so that I get a new string like the following: > s2 = 'hi:cat,bye:dog' > Is there a common recipe to accomplish that?

Re: alternating string replace

2008-01-09 Thread Peter Otten
cesco wrote: > say I have a string like the following: s1 = 'hi_cat_bye_dog' > and I want to replace the even '_' with ':' and the odd '_' with ',' so > that I get a new string like the following: s2 = 'hi:cat,bye:dog' >>> import re >>> from itertools import cycle >>> re.sub("_", lambda m, c=cycl

Re: alternating string replace

2008-01-09 Thread cokofreedom
On Jan 9, 11:34 am, cesco <[EMAIL PROTECTED]> wrote: > Hi, > > say I have a string like the following: > s1 = 'hi_cat_bye_dog' > and I want to replace the even '_' with ':' and the odd '_' with ',' > so that I get a new string like the following: > s2 = 'hi:cat,bye:dog' > Is there a common recipe t

Re: alternating string replace

2008-01-09 Thread Fredrik Lundh
cesco wrote: > and I want to replace the even '_' with ':' and the odd '_' with ',' > so that I get a new string like the following: > s2 = 'hi:cat,bye:dog' > Is there a common recipe to accomplish that? I can't come up with any > solution... how about splitting on "_", joining pairs with ":", an

alternating string replace

2008-01-09 Thread cesco
Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution... Thanks in a

Re: string replace shortcut

2007-09-18 Thread Gabriel Genellina
En Tue, 18 Sep 2007 08:44:32 -0300, <[EMAIL PROTECTED]> escribi�: > Hello, > > I am trying to replace some string with list objects: > my_text1="function1 function2" from my_module_with_functions_1 import * from my_module_with_functions_2 import * > > # functions in module " my_mo

string replace shortcut

2007-09-18 Thread vedrandekovic
Hello, I am trying to replace some string with list objects: >>> my_text1="function1 function2" >>> from my_module_with_functions_1 import * >>> from my_module_with_functions_2 import * # functions in module " my_module_with_functions_1 ": my_func1 it's value "function1" my_func2 it's valu

Re: Trouble fixing a broken ASCII string - "replace" mode in codec not working.

2007-02-06 Thread Neil Cerutti
On 2007-02-06, Robert Kern <[EMAIL PROTECTED]> wrote: > John Nagle wrote: >> File "D:\projects\sitetruth\InfoSitePage.py", line 285, in httpfetch >> sitetext = sitetext.encode('ascii','replace') # force to clean ASCII >> >> UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in >> pos

Re: Trouble fixing a broken ASCII string - "replace" mode in codec not working.

2007-02-06 Thread Robert Kern
John Nagle wrote: > I'm trying to clean up a bad ASCII string, one read from a > web page that is supposedly in the ASCII character set but has some > characters above 127. And I get this: > > File "D:\projects\sitetruth\InfoSitePage.py", line 285, in httpfetch > sitetext = sitetext.en

Trouble fixing a broken ASCII string - "replace" mode in codec not working.

2007-02-06 Thread John Nagle
I'm trying to clean up a bad ASCII string, one read from a web page that is supposedly in the ASCII character set but has some characters above 127. And I get this: File "D:\projects\sitetruth\InfoSitePage.py", line 285, in httpfetch sitetext = sitetext.encode('ascii','replace') # for

Re: String Replace only if whole word?

2006-11-19 Thread Leonhard Vogt
Michael Yanowitz schrieb: > Yeah, I am operating this on a Python script. However, I am working off > a requirement that the script be pre-processed and the strings replaced > before executing the script and that if there are any remaining (not > replaced) > names that I don't execute the script

Re: String Replace only if whole word?

2006-11-17 Thread Tim Chase
> I have been using the string.replace(from_string, to_string, len(string)) > to replace names in a file with their IP address. > For example, I have definitions file, that looks something like: > 10.1.3.4 LANDING_GEAR > 20.11.222.4 ALTIMETER_100 > 172.18.50.138 SIB > 172.18.50.138 LAPTOP >

RE: String Replace only if whole word?

2006-11-17 Thread Michael Yanowitz
>Michael Yanowitz wrote: >> Hello: >> >> I am hoping someone knows if there is an easier way to do this or someone >> already implemented something that does this, rather than reinventing the >> wheel: >> I have been using the string.replace(from_string, to_string, len(string)) >> to replace na

Re: String Replace only if whole word?

2006-11-17 Thread Juho Schultz
Michael Yanowitz wrote: > Hello: > > I am hoping someone knows if there is an easier way to do this or someone > already implemented something that does this, rather than reinventing the > wheel: > I have been using the string.replace(from_string, to_string, len(string)) > to replace names in a

String Replace only if whole word?

2006-11-17 Thread Michael Yanowitz
Hello: I am hoping someone knows if there is an easier way to do this or someone already implemented something that does this, rather than reinventing the wheel: I have been using the string.replace(from_string, to_string, len(string)) to replace names in a file with their IP address. For ex

Re: string replace

2006-07-01 Thread Michele Petrazzo
[EMAIL PROTECTED] wrote: > Check out the .translate method and the string.maketrans documentation. > You can use it to delete a list of characters all in one line: > Yes. This is, more or less, what I were looking for. P.s. Sure, if replace could accept a tuple... :) Thanks to all, Michele --

Re: string replace

2006-06-30 Thread johnzenger
Check out the .translate method and the string.maketrans documentation. You can use it to delete a list of characters all in one line: >>> s = "I am the walrus" >>> import string >>> s.translate(string.maketrans("",""),"aeiou") 'I m th wlrs' Michele Petrazzo wrote: > Hi, > a lot of times I nee

Re: string replace

2006-06-30 Thread Jim Segrave
In article <[EMAIL PROTECTED]>, Michele Petrazzo <[EMAIL PROTECTED]> wrote: >Hi, >a lot of times I need to replace more than one char into a string, so I >have to do something like > >value = "test" >chars = "e" >for c in chars: > value = value.replace(c, "") > >A solution could be that "replace

Re: string replace

2006-06-30 Thread Tim Chase
> a lot of times I need to replace more than one char into a > string, so I have to do something like > > value = "test" > chars = "e" > for c in chars: >value = value.replace(c, "") > > A solution could be that "replace" accept a tuple/list of > chars, like that was add into the new 2.5 for

Re: string replace

2006-06-30 Thread Schüle Daniel
> A solution could be that "replace" accept a tuple/list of chars, like > that was add into the new 2.5 for startswith. > > I don't know, but can be this feature included into a future python > release? I don't know, but I think it would be useful as for now I use this >>> import re >>> cha

RE: string replace

2006-06-30 Thread Cihal Josef
half Of Michele Petrazzo Sent: Friday, June 30, 2006 4:03 PM To: python-list@python.org Subject: string replace Hi, a lot of times I need to replace more than one char into a string, so I have to do something like value = "test" chars = "e" for c in chars: value = value.replace

string replace

2006-06-30 Thread Michele Petrazzo
Hi, a lot of times I need to replace more than one char into a string, so I have to do something like value = "test" chars = "e" for c in chars: value = value.replace(c, "") A solution could be that "replace" accept a tuple/list of chars, like that was add into the new 2.5 for startswith. I d

Re: Newbie question: string replace

2005-11-16 Thread Don
Diez B. Roggisch wrote: > [EMAIL PROTECTED] wrote: > > I have a config file with the following contents: > > service A = { > > params { > > dir = "c:\test", > > username = "test", > > password = "test" > > } > > } > > > > I want to find username and replace the value with another v

Re: Newbie question: string replace

2005-10-28 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Fri, 28 Oct 2005 12:27:36 -0700, [EMAIL PROTECTED] wrote: > > > hm...Is there a way to get rid of the newline in "print"? > > Yes, by using another language *wink* Ending the print statement with a comma also works;-) > Or, instead of using pri

Re: Newbie question: string replace

2005-10-28 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote: > "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > > > So how to overwrite the config file directly in script.py instead of > > running script.py with two params? > > Don't overwrite the file directly. Save a copy, then rename it. That See module fileinput

Re: Newbie question: string replace

2005-10-28 Thread Steven D'Aprano
On Fri, 28 Oct 2005 12:27:36 -0700, [EMAIL PROTECTED] wrote: > hm...Is there a way to get rid of the newline in "print"? Yes, by using another language *wink* Or, instead of using print, use sys.stdout.write(). -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Newbie question: string replace

2005-10-28 Thread [EMAIL PROTECTED]
Got it, thanks all! -- http://mail.python.org/mailman/listinfo/python-list

Re: Newbie question: string replace

2005-10-28 Thread [EMAIL PROTECTED]
hm...Is there a way to get rid of the newline in "print"? -- http://mail.python.org/mailman/listinfo/python-list

Re: Newbie question: string replace

2005-10-28 Thread Mike Meyer
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > Now it works: > rex = re.compile(r'(^.*username *=[^"]*")([^"]*)(".*$)') > for line in fileinput.input(FILE, inplace=1): > m = rex.match(line) > if m is not None: > line = "%s%s%s\n" % (m.group(1), new_name, m.group(3)) > pr

Re: Newbie question: string replace

2005-10-28 Thread Steve Holden
[EMAIL PROTECTED] wrote: > Now it works: > rex = re.compile(r'(^.*username *=[^"]*")([^"]*)(".*$)') > for line in fileinput.input(FILE, inplace=1): > m = rex.match(line) > if m is not None: > line = "%s%s%s\n" % (m.group(1), new_name, m.group(3)) > print line > > But there

Re: Newbie question: string replace

2005-10-28 Thread [EMAIL PROTECTED]
Now it works: rex = re.compile(r'(^.*username *=[^"]*")([^"]*)(".*$)') for line in fileinput.input(FILE, inplace=1): m = rex.match(line) if m is not None: line = "%s%s%s\n" % (m.group(1), new_name, m.group(3)) print line But there is an extra line break after each line in F

Re: Newbie question: string replace

2005-10-25 Thread Mike Meyer
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > So how to overwrite the config file directly in script.py instead of > running script.py with two params? Don't overwrite the file directly. Save a copy, then rename it. That way, you don't replace the new version until you know the old version is

Re: Newbie question: string replace

2005-10-25 Thread [EMAIL PROTECTED]
So how to overwrite the config file directly in script.py instead of running script.py with two params? A XML file or ConfigParser is appealing but this is part of a legacy system...:( -- http://mail.python.org/mailman/listinfo/python-list

Re: Newbie question: string replace

2005-10-25 Thread davideugenewarren
[EMAIL PROTECTED] wrote: > I have a config file with the following contents: > service A = { > params { > dir = "c:\test", > username = "test", > password = "test" > } > } > > I want to find username and replace the value with another value. I > don't know what the username value i

Re: Newbie question: string replace

2005-10-25 Thread Steven D'Aprano
On Tue, 25 Oct 2005 09:37:09 -0700, [EMAIL PROTECTED] wrote: > I have a config file with the following contents: > service A = { > params { > dir = "c:\test", > username = "test", > password = "test" > } > } > > I want to find username and replace the value with another value. I >

Re: Newbie question: string replace

2005-10-25 Thread Larry Bates
[EMAIL PROTECTED] wrote: > I have a config file with the following contents: > service A = { > params { > dir = "c:\test", > username = "test", > password = "test" > } > } > > I want to find username and replace the value with another value. I > don't know what the username value i

Re: Newbie question: string replace

2005-10-25 Thread Diez B. Roggisch
[EMAIL PROTECTED] wrote: > I have a config file with the following contents: > service A = { > params { > dir = "c:\test", > username = "test", > password = "test" > } > } > > I want to find username and replace the value with another value. I > don't know what the username value i

Newbie question: string replace

2005-10-25 Thread [EMAIL PROTECTED]
I have a config file with the following contents: service A = { params { dir = "c:\test", username = "test", password = "test" } } I want to find username and replace the value with another value. I don't know what the username value in advance though. How to do it in python? --

Re: count string replace occurances

2005-06-13 Thread William Park
Xah Lee <[EMAIL PROTECTED]> wrote: > if i have > mytext.replace(a,b) > how to find out many many occurances has been replaced? If 'a' and 'b' are different length, - Count the string length, before and after. The difference should be multiple of difference between length of 'a' and 'b'.

Re: count string replace occurances

2005-06-12 Thread George Sakkis
"Jeff Epler" wrote: > On Sun, Jun 12, 2005 at 04:55:38PM -0700, Xah Lee wrote: > > if i have > > mytext.replace(a,b) > > how to find out many many occurances has been replaced? > > The count isn't returned by the replace method. You'll have to count > and then replace. > > def count_replace(a, b,

Re: count string replace occurances

2005-06-12 Thread Jeff Epler
On Sun, Jun 12, 2005 at 04:55:38PM -0700, Xah Lee wrote: > if i have > mytext.replace(a,b) > how to find out many many occurances has been replaced? The count isn't returned by the replace method. You'll have to count and then replace. def count_replace(a, b, c): count = a.count(b) retur

count string replace occurances

2005-06-12 Thread Xah Lee
if i have mytext.replace(a,b) how to find out many many occurances has been replaced? Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ -- http://mail.python.org/mailman/listinfo/python-list

[OT] Re: String Replace Problem...

2005-02-28 Thread Steven Bethard
Sean McIlroy wrote: Alright, now it's too much. It's not enough that you're eliminating it from the language, you have to stigmatize the lambda as well. You misunderstand me. I don't have a problem with lambda when it's appropriate, e.g. when used as an expression, where a statement is forbidden

Re: String Replace Problem...

2005-02-28 Thread Sean McIlroy
Alright, now it's too much. It's not enough that you're eliminating it from the language, you have to stigmatize the lambda as well. You should take some time to reflect that not everybody thinks the same way. Those of us who are mathematically inclined like the lambda because it fits in well with

Re: String Replace Problem...

2005-02-28 Thread Steven Bethard
Sean McIlroy wrote: f = lambda x: (x[0]=='@' and x[6:] + '.0') or (x=='/' and x + '\n') or x See "Inappropriate use of Lambda" in http://www.python.org/moin/DubiousPython. You're creating a named function, so there's no reason to use the anonymous function syntax. Try: def f(x): return (x[

Re: String Replace Problem...

2005-02-28 Thread Sean McIlroy
I can't claim to have studied your problem in detail, but I get reasonable results from the following: filename = 'Errors.txt' S = open(filename,'r').read().split() f = lambda x: (x[0]=='@' and x[6:] + '.0') or (x=='/' and x + '\n') or x open(filename,'w').write(' '.join(map(f,S))) HTH -

Re: String Replace Problem...

2005-02-28 Thread wes weston
[EMAIL PROTECTED] wrote: Hello NG, probably this is a basic question, but I'm going crazy... I am unable to find an answer. Suppose that I have a file (that I called "Errors.txt") which contains these lines: MULTIPLY 'PERMX' @PERMX1 1 34 1 20 1 6 / 'PERMX' @PERMX2 1 34 21 41

String Replace Problem

2005-02-28 Thread andrea . gavana
Hello Peter And NG, thank you a lot... now I'm blaming myself, how couldn't I see it? I will probably go for the re.sub (if it is not too complicated, I'm not a Python expert and even less expert of RegEx things), if not I'll try your suggestion of sorting the strings by length. Thanks a lo

Re: String Replace Problem...

2005-02-28 Thread Peter Hansen
[EMAIL PROTECTED] wrote: 'PERMX' @PERMX1 1 34 1 20 1 6 / ... 'PERMX' @PERMX10 1 34 21 41 29 34/ ... I would like to replace all the occurrencies of the "keywords" (beginning with the @ (AT) symbol) with some floating point value. As an example, this is what I do: # Find All Keyw

String Replace Problem...

2005-02-28 Thread andrea . gavana
Hello NG, probably this is a basic question, but I'm going crazy... I am unable to find an answer. Suppose that I have a file (that I called "Errors.txt") which contains these lines: MULTIPLY 'PERMX' @PERMX1 1 34 1 20 1 6 / 'PERMX' @PERMX2 1 34 21 41 1 6 / 'PERMX' @P