change a string in text file then write it
Alex Martelli
aleaxit at yahoo.com
Mon Nov 6 15:21:52 EST 2000
"Carel Fellinger" <cfelling at iae.nl> wrote in message
news:8u6omp$3pu$1 at animus.fel.iae.nl...
> 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 ',' :)
<humble attitude='contrite'> yes, I keep doing that (AND omitting
other needed trailing stuff as well -- see a recent post where I
had managed to match 'all strings starting with 0 or more digits'...!-).
</humble>
<bluff attitude='genial'> Actually, I did it on purpose (what, ME
fall into such a simple trap?!) to see if anybody would notice --
Carel wins the prize, and a special Jury mention for the excellent
explanation that follows...:
</bluff>
> 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.
Alex
More information about the Python-list
mailing list