Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file access peek ahead

Tags:

python

I need to read a file, line by line and I need to peek into 'the next line' so first I read the file into a list and then I cycle throught the list... somehow this seems rude, building the list could be become expensive.

for line in open(filename, 'r'):
    lines.append(line[:-1])

for cn in range(0, len(lines)):
    line = lines[cn]
    nextline = lines[cn+1] # actual code checks for this eof overflow

there must be a better way to iterate over the lines but I don't know how to peek ahead

like image 802
Paul Avatar asked Jul 06 '12 09:07

Paul


2 Answers

You might be looking for something like the pairwise recipe from itertools.

from itertools import tee, izip
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

with open(filename) as f: # Remember to use a with block so the file is safely closed after
    for line, next_line in pairwise(f):
        # do stuff
like image 89
jamylak Avatar answered Nov 12 '22 21:11

jamylak


You could do it this way

last_line = None

for line in open(filename):                                                                  
    if last_line is not None:
        do_stuff(last_line, line) 
    last_line = line                                                        
like image 1
Otto Allmendinger Avatar answered Nov 12 '22 22:11

Otto Allmendinger