Entry
What is the best way to make multiple replacements of text strings in a file?
Jul 5th, 2000 05:37
Steve Holden, Thiébaut Champenier, unknown unknown, Thiébaut Champenier
You could 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.
How about using a dictionary and a loop:
replaceme = {'foo1': 'bar1', 'foo2': 'bar2', 'foo3': 'bar3'}
for key in replaceme.keys():
srch = key
rplc = replaceme[key]
contents = string.replace(contents, srch, rplc)
-------------------
Perhaps a bit complex for a newbie, but the most definitive answer I've
seen so far has been Fredrik Lundh's 2021-04-12 reply to the "Python
idiom: Multiple search-and-replace" thread:
> Is there a Python feature or standard library API that will get me
> less Python code spinning inside this loop? re.multisub or
> equivalent?
haven't benchmarked it, but I suspect that this approach is more
efficient:
...
# based on re-example-5.py
import re
import string
symbol_map = { "foo": "FOO", "bar": "BAR" }
def symbol_replace(match, get=symbol_map.get):
return get(match.group(1), "")
symbol_pattern = re.compile(
"(" + string.join(map(re.escape, symbol_map.keys()), "|") + ")"
)
print symbol_pattern.sub(symbol_replace, "foobarfiebarfoo")
-------------------
Thiébaut Champenier has wriiten a string replacement utility,
which is available for inspection at: http://tibi.free.fr/repl.py