multiple replacement values in python3
I have a few hundred thousand wonky values in a fixed width file. I want
to find the strings old_values and replace them with the strings in the
corresponding position in new_values. I could loop through and do this one
at a time, but I'm nearly certain there is a much faster way that I am not
expert enough to know about.
old_values = ('0000}', '0000J', '0000K', '0000L', '0000M', '0000N') # and
many more
new_values = (' -0', ' -1', ' -2', ' -3', ' -4', ' -5') # and
many more
file_snippet =
'00000000000000010000}0000000000000000000200002000000000000000000030000J0000100000000000000500000000000000000000000'
# each line is >7K chars long and there are over 6 gigs of text data
Looping through each value and running .replace on each line seems slow. eg:
for x in len(old_values):
line.replace(old_values[x], new_values[x])
Any tips for speeding things up?
No comments:
Post a Comment