I have a CSV file that I’m parsing with the help of pandas and casting to a dict like so: contacts = read_csv(file_handle).to_dict()
. So contacts
is now a dict, and contains the following keys:
(Pdb) contacts.keys()
['FirstName', 'Title', 'LastName', 'EmailAddress']
There’s a few thousand rows, and I need to access them in some way so I tried many, many different for loops, one is:
for i, name in enumerate(contacts['FirstName'].values()):
parsed_contacts.append(dict(
first_name=name,
last_name=...
))
(the above example has been shortened for display purposes) Now this works correctly, until the very last row, on which it throws a KeyError on FirstName:
File "/app/importer/contacts.py", line 290, in parse_contacts
for i, name in enumerate(contacts['FirstName'].values()):
KeyError: 'FirstName'
this makes no sense to me: it literally works for 4041 rows and fails at row 4042 with a KeyError
Same thing if I access it in some other way:
for i in range(len(contacts['FirstName'].values())):
parsed_contacts.append(dict(
first_name=contacts['FirstName'][i],
Not sure what’s going on there, it’s driving me crazy because these work if I run them interactively in pdb:
(Pdb) for i, name in enumerate(contacts['FirstName'].values()): print
name
John
Juliette
...
nan
Jose
Jesús
Frances
(Pdb) for i in range(len(contacts['FirstName'].values())): print i
0
...
4040
4041
Any ideas why this happens?