[Tutor] newbie search and replace
Magnus Lycka
magnus@thinkware.se
Mon Jan 27 18:37:01 2003
At 21:12 2003-01-27 +0100, Gregor Lingl wrote:
>>addrN.replace('LN', 'Lane')
>The readlines() method outputs a list of strings, whereas replace() is a
>string-method.
>If you used addr.read() instead, which reads in the content of the file as
>a string, the
>replace()-method should work.
But beware! Remember that ALL occurences of LN etc will be
replaced. And it's case sensitive.
>>> "First Liston St".replace('st', 'street')
'Firstreet Listreeton St'
Oops!
Regular expressions might be a better choice.
>>> import re
>>> stPat = re.compile(r'\bst\b', re.IGNORECASE)
>>> stPat.sub('Street', "First Liston St")
'First Liston Street'
But to use re well, you need some study and practice. See
the python library reference.
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se