python - KeyError raised while CSV file is correctly placed and keys exist in the CSV file -


i have python code:

import csv  csvfile = 'stations-nl-2014-01.csv'  try:     f = open(csvfile, 'r')     reader = csv.dictreader(f, delimiter=';')      row in reader:         print(row['name'], '--', row['type']) finally:     f.close() 

i can use regular csv.reader() method, told using csv.dictreader() best practise because can select columns names makes code more readable. however, when use normal csv.reader() method, no error shown. when use dictreader, following error:

print(row['name'], '--', row['type']) keyerror: 'name'

when change code to:

import csv  csvfile = 'stations-nl-2014-01.csv'  try:     f = open(csvfile, 'r')     reader = csv.reader(f, delimiter=';')      row in reader:         print(row) finally:     f.close() 

...everything works fine. using csv.dictreader() , selecting rows column names raises error. know how fix this? i've double checked path of .csv file , column names , it's correct (works fine without dictreader).

1) searched file 'stations-nl-2014-01.csv' on web, , see not have column 'name'. column 'type' exists though.

it has following columns: code uic naam middel_naam korte_naam rdt_url type geo_lat geo_lng

are sure shouldn't looking row['naam']?

2) if above doesn't work, can list of keys 'row' dictionary contains using row.keys(). give list of keys csv.dictreader recognized.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -