Read a record instead of a line from a file

Andrew Dalke dalke at
Fri Aug 24 12:55:46 CEST 2001

YMK wrote:
>If I know the "Record Separator" of a flat file, how do I set to read
>one record at a time ?

Here's something I just tried out using 2.2's 'yield' statement.
(2.2 is currently in alpha release.)  Warning: this is my first
generator and I've also not fully tested it.

from __future__ import generators

def SepReader(infile, sep = "\n\n"):
    text =
    if not text:
    while 1:
        fields = text.split(sep)
        for field in fields[:-1]:
            yield field
        text = fields[-1]
        new_text =
        if not new_text:
            yield text
        text += new_text

It's used like this

for record in SepReader(open(fortunes), "%\n"):
    print record

If you want something that's really high speed, but uses the
mxTextTools C extension, you can try my Martel parser, which
is part of  The specific record readers are in

                    dalke at

More information about the Python-list mailing list