readlines() and "binary" files
aleax at aleax.it
Thu Sep 26 00:13:25 CEST 2002
> 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 = ''
item, buf = buf.split(splitter, 1)
more = fileobj.read(bufsize)
if not more: break
buf += more
yield item + splitter
I think it's more or less the simplest approach.
More information about the Python-list