Python Forum
Load Comma Delimited csv to Nested Dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Load Comma Delimited csv to Nested Dictionary
#1
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.

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?)
    ?
Thanks for any help.

Reply
#2
I think the csv.DictReader is exactly what you want...
>>> import csv
>>> with open('Hitchhiker.csv', 'rt') as f:
...     reader = csv.DictReader(f, delimiter=',')
...     for row in reader:
...         print(row)
...     print(f'Header: {reader.fieldnames}')
Output:
OrderedDict([('Name', 'Ford Prefect'), ('Gender', 'Male'), ('Occupation', 'Researcher'), ('Home Planet', 'Betelgeuse Seven')]) OrderedDict([('Name', 'Arthur Dent'), ('Gender', 'Male'), ('Occupation', 'Sandwich-Maker'), ('Home Planet', 'Earth')]) OrderedDict([('Name', 'Tricia McMillan'), ('Gender', 'Female'), ('Occupation', 'Mathematician'), ('Home Planet', 'Earth')]) OrderedDict([('Name', 'Marvin'), ('Gender', 'Unknown'), ('Occupation', 'Paranoid Android'), ('Home Planet', 'Unknown')]) Header: ['Name', 'Gender', 'Occupation', 'Home Planet']
Reply
#3
Thanks, killerrex. That looks like what I want. I'm still not sure how to specify the column name by heading.
I will need something like...
if row[Occupation] == 'Mathematician':
This is my latest attempt which is obviously wrong and gets a syntax error..
import csv
with open(r'C:\Users\delliott\Desktop\pythoncsv\Q3\Hitchhiker.csv', 'rt') as f:
    reader = csv.DictReader(f, delimiter=',')
    for row in reader:
        if (row (f'Header: {reader.fieldnames.Occupation}) == 'Mathematician'):
            print('42')
        print(row)
    print(f'Header: {reader.fieldnames}')

I finally got it with the square brackets...

if row ['Occupation'] == 'Mathematician':
import csv
with open(r'C:\Users\delliott\Desktop\pythoncsv\Q3\Hitchhiker.csv', 'rt') as f:
    reader = csv.DictReader(f, delimiter=',')
    for row in reader:
        if row ['Occupation'] == 'Mathematician':
            print('42')
        print(row)
    print(f'Header: {reader.fieldnames}')
Thanks for your help. Your reply got me started.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 580 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 914 Nov-17-2025, 11:47 AM
Last Post: Winfried
  Tab Delimited Strings? johnywhy 7 3,275 Jan-13-2024, 10:34 PM
Last Post: sgrey
  need to compare 2 values in a nested dictionary jss 2 2,974 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 2,434 Aug-19-2022, 08:07 PM
Last Post: Winfried
  How to format Excel column with comma? dee 0 2,443 Jun-13-2022, 10:11 PM
Last Post: dee
  decimal comma DPaul 9 4,990 Feb-22-2022, 12:25 PM
Last Post: DeaD_EyE
  dataframe write to tab delimited differs from Excel koh 0 3,108 Aug-01-2021, 02:46 AM
Last Post: koh
  Adding a comma in the resulting value stsxbel 6 4,711 May-22-2021, 09:24 PM
Last Post: stsxbel
  Nested dictionary acting strange Pedroski55 2 3,645 May-13-2021, 10:37 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020