Python idiom: Multiple search-and-replace

David Goodger dgoodger at bigfoot.com
Wed Apr 12 23:21:54 EDT 2000


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

BTW, excellent idiom (especially the first one, represented by '...' above).
I can put it to immediate use. You've just sold another copy of your book
... as soon as it's Mac-friendly. And can I copy & paste from it? If not,
maybe I'll wait for the tree-killing edition.

-- 
David Goodger    dgoodger at bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)




More information about the Python-list mailing list