string.replace() or re.subn()

bragib at my-deja.com bragib at my-deja.com
Fri Sep 1 11:03:23 EDT 2000


Hi:

I have the following problem where I am replacing periods in certain
names in a text file by underscores.  So for example if I have
these names in the file [set.1, set.1.1] I would like to replace
them everywhere by set_1 and set_1_1.  Now the catch is I can
have a line like this

line = '1.0, 2.0, set.1, set.1.1'

for name in ['set.1', 'set.1.1']:
    line = string.replace(line, name, string.replace(name,'.','_'))
    print line

1.0, 2.0, set_1, set_1.1
1.0, 2.0, set_1, set_1.1

which is not what I wanted.  I moved away from using re.sub because the
names can potentially contain characters such as
!@#$%^&*()_-+={}[]\|~`?/<>.,

Any help on this would be greatly appreciated.



Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list