5.11 xreadlines -- Efficient iteration over a file

New in version 2.1.

This module defines a new object type which can efficiently iterate over the lines of a file. An xreadlines object is a sequence type which implements simple in-order indexing beginning at 0, as required by for statement or the filter() function.

Thus, the code

import xreadlines, sys

for line in xreadlines.xreadlines(sys.stdin):
    pass

has approximately the same speed and memory consumption as

while 1:
    lines = sys.stdin.readlines(8*1024)
    if not lines: break
    for line in lines:
        pass

except the clarity of the for statement is retained in the former case.

xreadlines(fileobj)
Return a new xreadlines object which will iterate over the contents of fileobj. fileobj must have a readlines() method that supports the sizehint parameter.

An xreadlines object s supports the following sequence operation:

5.11 xreadlines -- Efficient iteration over a file
Previous Page Up One Level Next Page Python Library Reference Contents Module Index</