1

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
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
KaraUmur
  • 11
  • 1
  • 2

1 Answers1

1

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)
mementum
  • 3,153
  • 13
  • 20