I would like to load a comma delimited csv file into a nested dictionary.
I'm unable to attach the csv file...
Name,Gender,Occupation,Home Planet
Ford Prefect,Male,Researcher,Betelgeuse Seven
Arthur Dent,Male,Sandwich-Maker,Earth
Tricia McMillan,Female,Mathematician,Earth
Marvin,Unknown,Paranoid Android,Unknown
I think this is the structure I want...
people[‘Arthur’]
{‘Occupation’: ‘Sandwich-Maker’, ‘Home Planet’: ‘Earth’, ‘Gender’: ‘Male’,
‘Name’: ‘Arthur Dent’}
Then I would like to get ‘Home Planet’ totals.
Betelgeuse Seven = 1
Earth = 2
Unknown = 1
Then I would like to print 42 if Occupation = ‘Mathematician’.
No, this is not a homework assignment. I’m new to Python and want to understand nested dictionaries and how to iterate through them.
Thanks for any help.
I'm unable to attach the csv file...
Name,Gender,Occupation,Home Planet
Ford Prefect,Male,Researcher,Betelgeuse Seven
Arthur Dent,Male,Sandwich-Maker,Earth
Tricia McMillan,Female,Mathematician,Earth
Marvin,Unknown,Paranoid Android,Unknown
I think this is the structure I want...
people[‘Arthur’]
{‘Occupation’: ‘Sandwich-Maker’, ‘Home Planet’: ‘Earth’, ‘Gender’: ‘Male’,
‘Name’: ‘Arthur Dent’}
Then I would like to get ‘Home Planet’ totals.
Betelgeuse Seven = 1
Earth = 2
Unknown = 1
Then I would like to print 42 if Occupation = ‘Mathematician’.
No, this is not a homework assignment. I’m new to Python and want to understand nested dictionaries and how to iterate through them.
1 2 3 4 5 |
import csv with open (r "C:\Hitchhiker.csv" , "r" ) as f: reader = csv.reader(f, delimiter = "," ) next (reader, None ) # skip the headers (Can I use the headers?) ? |