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
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
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                                                        
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With