Python equivalent of Perl's Input Record Separator ?
Alex Martelli
aleax at aleax.it
Wed Feb 12 08:09:10 EST 2003
remco boerma wrote:
>> I have a large file with records ending in ox0a 0x0a, the records also
>> contain \n's
>
> If the file is not to long you could use:
>
> f = file('filename')
> sep = chr(0x0a) *2
> records = file.read().split(sep)
> f.close
Error: use f.close()
the trailing parenteses are needed.
> print 'File has', len(records),'records'
Sure, but "large file" normally means one that won't fit comfortably
in memory, and here you'd need it to fit comfortably in HALF of
memory -- that's why I gave a generator-based solution, with low
memory consumption, not tied to the file size but only to the maximum
size of each record.
Alex
More information about the Python-list
mailing list