Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have csv.reader tell when it is on the last line

Tags:

python

csv

Apparently some csv output implementation somewhere truncates field separators from the right on the last row and only the last row in the file when the fields are null.

Example input csv, fields 'c' and 'd' are nullable:

a|b|c|d
1|2||
1|2|3|4
3|4||
2|3

In something like the script below, how can I tell whether I am on the last line so I know how to handle it appropriately?

import csv

reader = csv.reader(open('somefile.csv'), delimiter='|', quotechar=None)

header = reader.next()

for line_num, row in enumerate(reader):
    assert len(row) == len(header)
    ....
like image 521
ʞɔıu Avatar asked Jun 09 '09 15:06

ʞɔıu


1 Answers

Basically you only know you've run out after you've run out. So you could wrap the reader iterator, e.g. as follows:

def isLast(itr):
  old = itr.next()
  for new in itr:
    yield False, old
    old = new
  yield True, old

and change your code to:

for line_num, (is_last, row) in enumerate(isLast(reader)):
    if not is_last: assert len(row) == len(header)

etc.

like image 170
Alex Martelli Avatar answered Nov 05 '22 17:11

Alex Martelli