any better way to do it?
niels at endea.demon.nl
Thu Mar 9 15:40:03 CET 2000
Benyang Tang schreef:
> Here I am doing some replacement in a file. It may just take one line in sed. I am wondering whether there is a shorter way in python to do it. Thanks for any suggestion.
For one thing, it's easier to read the whole file into a single string:
lines = open('data').read()
lines = re.sub(r'^\s*nStateStart\s*=\s*(\d+)', '123', lines)
lines = re.sub(r'^\s*nStateStop\s*=\s*(\d+)', '223', lines)
You can do this in one line, if you like. Note that there is a slight
difference in semantics, because '\s' also matches a newline, but that
is easy to fix, if you want to. BTW, is this really the substitution you
want, or something like
lines = re.sub(r'^(\s*nStateStart\s*=\s*)\d+', '\g<1>123', lines)
More information about the Python-list