Python idiom: Multiple search-and-replace

Fredrik Lundh effbot at telia.com
Wed Apr 12 18:25:32 CEST 2000


Randall Hopper <aa8vb at yahoo.com> wrote:
> Thanks!  It's much more efficient.  The 140 seconds original running time
> was reduced to 11.6 seconds.  I can certainly live with that.

thought so ;-)

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...

</F>





More information about the Python-list mailing list