first, second, etc line of text file
Neil Cerutti
horpner at yahoo.com
Thu Jul 26 09:23:15 EDT 2007
On 2007-07-25, George Sakkis <george.sakkis at gmail.com> wrote:
> For random access, the easiest way is to slurp all the file in
> a list using file.readlines().
A lazy evaluation scheme might be useful for random access that
only slurps as much as you need.
class LazySlurper(object):
r""" Lazily read a file using readline, allowing random access to the
results with __getitem__.
>>> import StringIO
>>> infile = StringIO.StringIO(
... "Line 0\n"
... "Line 1\n"
... "Line 2\n"
... "Line 3\n"
... "Line 4\n"
... "Line 5\n"
... "Line 6\n"
... "Line 7\n")
>>> slurper = LazySlurper(infile)
>>> print slurper[0],
Line 0
>>> print slurper[5],
Line 5
>>> print slurper[1],
Line 1
>>> infile.close()
"""
def __init__(self, fileobj):
self.fileobj = fileobj
self.upto = 0
self.lines = []
self._readupto(0)
def _readupto(self, n):
while self.upto <= n:
line = self.fileobj.readline()
if line == "":
break
self.lines.append(line)
self.upto += 1
def __getitem__(self, n):
self._readupto(n)
return self.lines[n]
--
Neil Cerutti
Eddie Robinson is about one word: winning and losing. --Eddie Robinson's agent
Paul Collier
More information about the Python-list
mailing list