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

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -