0

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]'}}
}
5
  • 1
    What do you need the desired result for? In this structure it looks like it's going to give you some troubles working with it. Commented Nov 8, 2020 at 5:00
  • I couldn't replicate the same results you had with the CSV you added. Commented Nov 8, 2020 at 5:05
  • I have json data in that structure which I plan to join to it. I thought that might be the best way to pull some exploratory data from the fields. If, for example, I want to know all of the unique countries in the set. Commented Nov 8, 2020 at 5:07
  • Why use dicts that have sequential integers for the keys, when you could just use a list? Commented Nov 8, 2020 at 5:34
  • I have three sets of data and thought to change one rather than 2. But it looks like I may have to change the others as well.. Commented Nov 8, 2020 at 5:46

1 Answer 1

2

Best way is to (ab)use Pandas ;-)

import pandas as pd
d = pd.DataFrame([result]).to_dict()
print(d)

For test case result:

{'1': {'Name': 'Bob Myers', 'Phone': '410-504-5887', 'Address': '12334 Hamilton Way', 'City': 'Toronto',
'Country': 'Canada', 'Email': '[email protected]'}

Output is like:

{'Name': {0: 'Bob Myers'}, 'Phone': {0: '410-504-5887'}, 'Address': {0: '12334 Hamilton Way'}, 'City': {0: 'Toronto'}, 'Country': {0: 'Canada'}, 'Email': {0: '[email protected]'}} 
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately here I cannot use pandas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.