Python equivalent of Perl's Input Record Separator ?

Alex Martelli aleax at
Wed Feb 12 11:01:49 CET 2003

Rich wrote:

> Is there a Python equivalent for $/ ?
> I have a large file with records ending in ox0a 0x0a, the records also
> contain \n's

Not directly, but it's not terribly hard to build e.g. a
generator that fulfils your wish.  For example (warning,
untested code!):

from __future__ import generators

def irser(fileob, separator):
    seplen = len(separator)
    block =

    while block:
        where = block.find(separator)

        if where < 0:
            moredata =
            if moredata:
                block += moredata
            yield block

        where += seplen
        yield block[:where]
        block = block[where:]

to be used as:

for record in irser(open('myf','rb'), '\x0A\x0A'):


More information about the Python-list mailing list