trouble with replace
Jason Nordwick
jason at adapt.com
Sat Aug 12 17:15:17 EDT 2006
>>> 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)
'newshighwayyuuuu\n\n\n monitorbyuuuterrfr'
f pemberton wrote:
> 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.
>
More information about the Python-list
mailing list