readlines() and "binary" files

Alex Martelli aleax at aleax.it
Thu Sep 26 00:13:25 CEST 2002


Justin wrote:
        ...
> at a time, so that's rather overkill. If anyone can think of a more
> economical way of doing it (short of defining my own iterator), I'd
> be interested.

Pity you ruled out defining your own iterator -- in Python 2.2.1...:

from __future__ import generators

def splitby(fileobj, splitter, bufsize=8192):
    buf = ''

    while True:
        try: 
            item, buf = buf.split(splitter, 1)
        except ValueError:
            more = fileobj.read(bufsize)
            if not more: break
            buf += more
        else:
            yield item + splitter

    if buf:
        yield buf


I think it's more or less the simplest approach.


Alex




More information about the Python-list mailing list