multiple replacements
Steve Holden
sholden at bellatlantic.net
Tue Jun 27 13:29:37 EDT 2000
siva1311 at my-deja.com wrote:
>
> Hi,
> I am a newbie Python programmer and was wondering the best way to make
> multiple replacements of text strings in a file. I have 10-15 strings to
> replace and right now I am doing this like:
>
> source = open(source_file,'r')
> contents = source.read()
> source.close()
>
> contents1 = string.replace(contents, 'foo1', 'bar1')
> contents2 = string.replace(contents1, 'foo2', 'bar2')
> ...
> contents10 = string.replace(contents9, 'foo10', 'bar10')
>
> dest = open(dest_file, 'w')
> dest.write(contents10)
> dest.close()
>
> I know this must be a terrible kludge.
>
Even so, if you only need to do this infrequently it's probably good
enough. An engineering approach... :-)
> Using sed I can use a single statement to carry out the replacements:
> sed "s/foo1/bar1/g
> s/foo2/bar2/g
> ...
> s/foo10/bar10/g" source_file > dest_file
>
> so I assume there must be a much better method than I am currently using
> in my Python version.
>
> Any suggestions for improvement to the Python code are appreciated.
>
> -Brad
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
Well, there's cleaning the source up and making it easier to extend,
and there's efficiency: you don't say which is your final goal.
You cold clean the source up a bit with:
foobar = ( ('foo1', 'bar1'),
('foo2', 'bar2'),
('fooN', 'barN') )
source = open(source_file,'r')
contents = source.read()
source.close()
for foo, bar in foobar:
contents = replace(contents, foo, bar)
dest = open(dest_file, 'w')
dest.write(contents10)
dest.close()
For speedup, you could write the whole loop as a single statement,
but it will get horrible quickly:
contents = replace(
replace(
replace(contents,
'fooN', 'barN'),
'foo2', 'bar2'),
'foo1', 'bar1)
and, of course, ths code is much less easy to maintain.
regards
Steve
--
Helping people meet their information needs with training and technology.
703 967 0887 sholden at bellatlantic.net http://home.cox.rr.com/sholden/
More information about the Python-list
mailing list