python - Index error, csv files -
let's have csv file in it:
luke,4 michael,6 if run code, returns 'indexerror: list index out of range':
import csv open('test.csv', 'r') f: reader = csv.reader(f) row in reader: n = row[0] print(n) if comment out n = row[0] , change print(row), prints out:
['luke', '4'] ['michael', '5'] [] that list causing problem i'm not sure how rid of it? appreciated!
this how handle empty rows when parsing:
import csv open('test.csv', 'r') f: reader = csv.reader(f) row in reader: if row: # excluding empty rows here. n = row[0] print(n)
Comments
Post a Comment