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
  • 155,494
  • 24
  • 217
  • 209
KaraUmur
  • 11
  • 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,023
  • 10
  • 20