imSonuGupta
imSonuGupta

Reputation: 925

Python 'KeyError: 0' while printing first column from csv

I'm new in python and trying read each row by column index but getting KeyError: 0 while executing below code:

with open('processed/test.csv') as f:
    reader = csv.DictReader(f) 
    for row in reader:
        print row[0]

Does any one have idea how to read column by index?

Upvotes: 1

Views: 1895

Answers (1)

mrvol
mrvol

Reputation: 2973

Why do you use DictReader, if you want to get row by index? Maybe your code should look like this?

with open('processed/test.csv') as f:
    reader = csv.reader(f) 
    for row in reader:
        print ', '.join(row)
        if len(row):
            print row[0]

Upvotes: 4

Related Questions