change a string in text file then write it
Carel Fellinger
cfelling at iae.nl
Mon Nov 6 12:10:49 EST 2000
Alex Martelli <aleaxit at yahoo.com> wrote:
...
> for line in fileinput.input("thefile.txt", inplace=1):
> if re.search(myre, line):
> line = string.replace(line,'foo','bar')
> print line
Ah, this must be one of Alex favorite typos, omitting a trailing ',' :)
You see, the last line should actually read:
print line, #note the trailing ","
the lines you get from "fileinput.input('thefile.txt', inplace=1)" have
a trailing end-of-line symbol embedded if it was there in the input file.
The print statement adds a trailing end-of-line symbol regardless of
whether the string to write ended with one, *unless* you end the print
statement with a comma, then this adding is omitted.
--
groetjes, carel
More information about the Python-list
mailing list