I have some data in a csv that when opened looks like this:
Example Data
['', 'Name', 'Phone', 'Address', 'City', 'Country', 'Email']
['1', 'Bob Myers', '410-504-5887', '12334 Hamilton Way', 'Toronto', 'Canada', '[email protected]']
['2', 'Carlton James', '455-323-8479', '1234 James Rd', 'New York', 'USA', '[email protected]']
['3', 'Frank Wright', '744-521-9874', '567 Travis St', 'Boston', 'USA', '[email protected]']
I want to create a nested dictionary where I take the integer in '' and use it as a key for each nested dictionary. I found a few similar questions and tried:
import csv
f = csv.DictReader(open('data.csv'))
result = {}
for row in f:
key = row.pop('')
result[key] = row
print(result)
Which yielded something like:
{'1': {'Name': 'Bob Myers', 'Phone': '410-504-5887', 'Address': '12334 Hamilton Way', 'City': 'Toronto',
'Country': 'Canada', 'Email': '[email protected]'}...
What do I change in my code to get my data to look something like this (without pandas):
my_dict = {{'Name': {1: 'Bob Myers', 2: 'Carlton James', 3: 'Frank Wright'}},
{'Phone': {1: '410-504-5887', 2: '455-323-8479', 3: '744-521-9874'}},
{'Address': {1: '12334 Hamilton Way', 2: '1234 James Rd', 3: '567 Travis St'}},
{'City': {1: 'Toronto', 2: 'New York', 3: 'Boston'}},
{'Country': {1: 'Canada', 2: 'USA', 3: 'USA'}},
{'Email': {1: '[email protected]', 2: '[email protected]', 3: '[email protected]'}}
}