Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get length of csv file without ruining reader?

Tags:

python

csv

I am trying to do the following:

reader = csv.DictReader(open(self.file_path), delimiter='|')
reader_length = sum([_ for item in reader])
for line in reader:
    print line

However, doing the reader_length line, makes the reader itself unreadable. Note that I do not want to do a list() on the reader, as it is too big to read on my machine entirely from memory.

like image 744
David542 Avatar asked Mar 10 '15 20:03

David542


1 Answers

Use enumerate with a start value of 1, when you get to the end of the file you will have the line count:

for count,line in enumerate(reader,1):
    # do work
print count

Or if you need the count at the start for some reason sum using a generator expression and seek back to the start of the file:

  with open(self.file_path) as f:
        reader = csv.DictReader(f, delimiter='|')
        count = sum(1 for _ in reader)
        f.seek(0)
        reader = csv.DictReader(f, delimiter='|')
        for line in reader:
            print(line)
like image 92
Padraic Cunningham Avatar answered Sep 27 '22 19:09

Padraic Cunningham