JimKM JimKM - 2 months ago 9
Python Question

CSV to Python Dictionary with multiple lists for one key

So I have a csv file formatted like this

data_a,dataA,data1,data11
data_b,dataB,data1,data12
data_c,dataC,data1,data13
, , ,
data_d,dataD,data2,data21
data_e,dataE,data2,data22
data_f,dataF,data2,data23
HEADER1,HEADER2,HEADER3,HEADER4


The column headers are at the bottom, and I want the third column to be the keys. You can see that the third column is the same value for each of the two blocks of data and these blocks of data are separated by empty values, so I want to store the 3 rows of values to this 1 key and also disregard some columns such as column 4. This is my code right now

#!usr/bin/env python

import csv

with open("example.csv") as f:
readCSV = csv.reader(f)

for row in readCSV:
# disregard separating rows
if row[2] != '':
myDict = {row[2]:[row[0],row[1]]}
print(myDict)


What I basically want is that when I call

print(myDict['data2'])


I get

{[data_d,dataD][data_e,dataE][data_f,dataF]}


I tried editing my if loop to

if row[2] == 'data2':
myDict = {'data2':[row[0],row[1]]}


and just make an if for every individual key, but I don't think this will work either way.

Answer Source

With your current method, you probably want a defaultdict. This is a dictionary-like object that provides a default value if the key doesn't already exist. So in your case, we set this up to be a list, and then for each row we loop through, we append the values in columns 0 and 1 to this list as a tuple, like so:

import csv
from collections import defaultdict

data = defaultdict(list)

with open("example.csv") as f:
    readCSV = csv.reader(f)

    for row in readCSV:
        # disregard separating rows
        if row[2] != '':
            data[row[2]].append((row[0], row[1]))

print(data)

With the example provided, this prints a defaultdict with the following entries:

{'data1': [('data_a', 'dataA'), ('data_b', 'dataB'), ('data_c', 'dataC')], 'data2': [('data_d', 'dataD'), ('data_e', 'dataE'), ('data_f', 'dataF')]}