Python idiom: Multiple search-and-replace
Fredrik Lundh
effbot at telia.com
Thu Apr 13 02:58:42 EDT 2000
David Goodger <dgoodger at bigfoot.com> wrote:
> on 2000-04-12 12:25, Fredrik Lundh (effbot at telia.com) wrote:
> > while you're at it, try replacing the original readline loop with:
> >
> > while 1:
> > lines = fp.readlines(BUFFERSIZE)
> > if not lines:
> > break
> > lines = string.join(lines, "")
> > lines = re.sub(...)
> > out_fp.write(lines)
> >
> > where BUFFERSIZE is 1000000 or so...
>
> why not just,
>
> while 1:
> lines = fp.read(BUFFERSIZE)
> if not lines:
> break
> lines = re.sub(...)
> out_fp.write(lines)
>
> ? Saves the string.join() step. Or am I missing something? (I await
> enlightenment...)
set BUFFERSIZE to 1
try replacing "foo" with "bar"
await enlightenment
(not that it cannot be fixed, of course. just add code to strip off
the last incomplete line and prepend it to the next read. make sure
you don't mess up if the last character in the file is not a newline)
</F>
More information about the Python-list
mailing list