How can I read a CSV into a Python dictionary, where each key's value is a list of dicts?

I have a CSV file (staff) that looks like this:

id, name, title
1, Steve, Customer Service Manager
2, Martin, Warehouse Operator
3, Jonathan, Backend Developer

I want to parse this into the format:

staff = {
            '1':[{'name':'Steve'}, {'title':'Customer Service Manager'}],
            '2':[{'name':'Martin'}, {'title':'Warehouse Operator'}],
            '3':[{'name':'Jonathan'}, {'title':'Backend Developer'}]
        }

But as far as I can tell, the csvreader and pandas libraries don't support this type of operation. Is there a clean way to do this, perhaps with comprehension?


I think DictReader may be a good solution:

with open("sample.csv", "r") as f_csv:
    reader = csv.DictReader(f_csv)
    data = [row for row in reader]
staff = {r["id"]: [{k: v} for k, v in r.items() if "id" not in k] for r in data}
print(staff)

output:

{'1': [{'name': 'Steve'}, {'title': 'Customer Service Manager'}], '2': [{'name': 'Martin'}, {'title': 'Warehouse Operator'}], '3': [{'name': 'Jonathan'}, {'title': 'Backend Developer'}]}

notes

I modified the csv not to have comma and space to separate fields. Also, this allows any number of other fields, rather than hardcoding just the two shown here. This also combines both dict and list comprehensions.


I created a CSV file and copied the exact data you shared, the following code is giving the desired results.

Code:

import csv
with open('some.csv', newline='') as f:
    reader = csv.reader(f)
    dict_ = {}
    for row in reader:
        if(row[0].isnumeric()):
           dict_[row[0]] = [{"name":row[1]},{"Title":row[2]}]
print(dict_)

Output:

{'1': [{'name': ' Steve'}, {'Title': ' Customer Service Manager'}], '2': [{'name': ' Martin'}, {'Title': ' Warehouse Operator'}], '3': [{'name': ' Jonathan'}, {'Title': ' Backend Developer'}]}

The reader returns a 2D list which is as follows :

[
['id', ' name', ' title'],
['1', ' Steve', ' Customer Service Manager'],
['2', ' Martin', ' Warehouse Operator'],
['3', ' Jonathan', ' Backend Developer']
]

The code operates on this list using a for loop to append the data in a dictionary in the needed format.


Here the code


import csv

with open('staff.csv', 'r') as f:
    reader = csv.reader(f)
    data = {}
    next(reader) # Skip header
    for row in reader:
        #print(f"Row : {row}")
        id,name,title = row
        data [id] = [{'name': name} , {'title': title}]

Exemple of output

{'1': [{'name': ' Steve'}, {'title': ' Customer Service Manager'}], '2': [{'name': ' Martin'}, {'title': ' Warehouse Operator'}], '3': [{'name': ' Jonathan'}, {'title': ' Backend Developer'}]}

If you want to remove the the space in name and title you can use the function strip.