Skip to content
Advertisement

‘KeyError’ while filling an SQL table with CSV data

I’m currently working on a project that’s requires wrangling OSM data (Udacity Course), and adding it to an SQLite dataset ready for queries.

I’m currently trying to add a ‘.csv’ file to an exisiting SQL table with the following function:

with open('crawley_nodes.csv','r', encoding = 'utf-8') as fin:
    dr = csv.DictReader(fin)
    to_db = [(i['id'], i['lat'], i['lon'], i['user'], i['uid'], i['version'], i['changeset'], i['timestamp']) for i in dr]
    cur.executemany("INSERT INTO nodes(id, lat, lon, user, uid, version, changeset, timestamp) VALUES (?,?,?,?,?,?,?,?);", to_db)

conn.commit()

However each time I try to execute it get the following error:

KeyError: 'id'

I’m bewildered as to why i’m getting this error. Perhaps the amount of time i’ve spent at the screen all day.

Thank you for any direction / help.

Advertisement

Answer

I found out where the error was. In fact the ‘.csv’ file i had prepared for the SQL database had a ‘b’ value next to each value. This is why it couldn’t find the keys I had identified.

To fix this I modified my osm to csv functions in order to create the csv’s correctly.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement