trouble with replace

Simon Forman rogue_pedro at yahoo.com
Sat Aug 12 14:01:21 EDT 2006


Simon Forman wrote:
> f pemberton wrote:
> > Marc 'BlackJack' Rintsch wrote:
> > > In <1155401390.926925.252000 at m79g2000cwm.googlegroups.com>, f pemberton
> > > wrote:
> > >
> > > > I've tried using replace but its not working for me.
> > > > xdata.replace('abcdef', 'highway')
> > > > xdata.replace('defgef', 'news')
> > > > xdata.replace('effwer', 'monitor')
> > >
> > > `replace()` does not work in place.  You have to bind the result to a name
> > > like::
> > >
> > >   xdata = xdata.replace('abcdef', 'highway')
> > >
> > > Ciao,
> > > 	Marc 'BlackJack' Rintsch
> >
> > lol, you probably will not believe me but I actually knew that already.
> >  I just forgot to add that part in my original post. When I try and
> > replace what happens is the first replace works fine but when I try and
> > do a second replace on a different part of the same string, the program
> > will print the string a second time(which I do not want).
>
> Um, don't print the string until after you're done replacing?  :-)
>
> Seriously though, since there are no print statements, etc.., in your
> posted code, it's hard to understand exactly what your problem is.
>
> > How can you
> > do 2 or more replaces in one line of code?
>
> In any event, there is a neat method of "single-pass multiple string
> substitution using a dictionary" here:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330  It uses
> a regular expression, so I'd guess it wouldn't be to hard to get it to
> work in multiline mode.
>
> Peace,
> ~Simon

Or just:

xdata = xdata.replace('abcdef', 'highway').replace('defgef',
'news').replace('effwer', 'monitor')

Hahahahaha

~Simon




More information about the Python-list mailing list