Python - How to read previous lines while reading csv file?
Question: I want to read rows in excel table but when I want, during reading process, I
would like to stop reading forward and I want to read previous lines (backward
reading)? How can I go previous rows again?

    
    
    import csv
    
    file = open('ff.csv2', 'rb')
    reader = csv.reader(file)
    
    for row in reader:
        print row
    


Answer: You could always store the lines in a list and then access each line using the
index.

    
    
    import csv
    file = open('ff.csv2', 'r')
    
    def somereason(line):
        # some logic to decide if stop reading by returning True
        return False  # keep on reading
    
    for row in csv.reader(file):
        if somereason(line):
            break
    
        lines.append(line)
    
    # visit the stored lines in reverse
    for row in reversed(lines):
       print(row)
    

