trouble with replace

F

f pemberton

I have a string (xdata) and theres a newline after every 17 characters
of the string. I was wondering how I can replace multiple substrings
multiple times within a string? To put it another way, this is what i
want to do.

Substring to find ("abcdef") replace it with ("highway")
search again, substring to find ("defgef") replace it with
("news").Search again and find ("effwer") replace it with
("monitor").Example string to search under is '''defgefabcdefyuuuu


effwerbyuuuterrfr'''

I've tried using replace but its not working for me.
xdata.replace('abcdef', 'highway')
xdata.replace('defgef', 'news')
xdata.replace('effwer', 'monitor')
Basically I would want to get the output of "newshighwayyuuuu


monitorbyuuuterrfr"

Thanks, any help would be appreciated.
 
M

Marc 'BlackJack' Rintsch

f pemberton said:
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
 
F

f pemberton

Marc said:
`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). How can you
do 2 or more replaces in one line of code?
 
S

Simon Forman

f said:
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
 
S

Simon Forman

Simon said:
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.


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
 
T

Tim Chase

pats = ['abcdef', 'defgef', 'effwer']
reps = ['highway', 'news', 'monitor']
s = 'defgefabcdefyuuuu\n\n\n effwerbyuuuterrfr'
reduce(lambda x,y: x.replace(*y), zip(pats,reps), s)


The reduce() method fairly works well if you have it as a
dictionary as well:
>>> m = {'effwer': 'monitor', 'abcdef': 'highway', 'defgef': 'news'}
>>> s = 'defgefabcdefyuuuu\n\n\n effwerbyuuuterrfr'
>>> reduce(lambda x,y: x.replace(y, m[y]), m.keys(), s)
'newshighwayyuuuu\n\n\n monitorbyuuuterrfr'

One does get somewhat unpredictable results if one of the
replacements contains a target search pattern (or creates it in
the resulting string):
>>> s = 'onetwothree'
>>> m = {'one':'two', 'two':'three', 'three':'one'}
>>> reduce(lambda x,y: x.replace(y, m[y]), m.keys(),s) 'twothreetwo'
>>> m['three'] = 'four'
>>> m['four'] = 'two'
>>> reduce(lambda x,y: x.replace(y, m[y]), m.keys(),s)
'twothreefour'

Just a few more ideas...

-tkc
 
A

Anthra Norell

This might well take the trouble out ot the OP's replace:

http://cheeseshop.python.org/pypi/SE/2.2 beta

Regards

Frederic

----------------------------------------------------------------------------------
And here is how it works:

effwerbyuuuterrfr'''newshighwayyuuuu

monitorbyuuuterrfr
----------------------------------------------------------------------------------

----- Original Message -----
From: "f pemberton" <[email protected]>
Newsgroups: comp.lang.python
To: <[email protected]>
Sent: Saturday, August 12, 2006 6:49 PM
Subject: trouble with replace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top