Python equivalent of Perl's Input Record Separator ?

Alex Martelli aleax at aleax.it
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 = fileob.read(8192)

    while block:
        where = block.find(separator)

        if where < 0:
            moredata = fileob.read(8192)
            if moredata:
                block += moredata
                continue
            yield block
            return

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

to be used as:

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


Alex





More information about the Python-list mailing list