I'm struggling with csv import to nested dictionary. I found a example thats almost perfect for me:
UID,BID,R
U1,B1,4
U1,B2,3
U2,B1,2
import csv
new_data_dict = {}
with open("data.csv", 'r') as data_file:
data = csv.DictReader(data_file, delimiter=",")
for row in data:
item = new_data_dict.get(row["UID"], dict())
item[row["BID"]] = int(row["R"])
new_data_dict[row["UID"]] = item
print new_data_dict
in my case I have one level of nesting more to do. my data looks like:
FID,UID,BID,R
A1,U1,B1,4
A1,U1,B2,3
A1,U2,B1,2
A2,U1,B1,4
A2,U1,B2,3
A2,U2,B1,2
Result should be:
{"A1":{"U1":{"B1":4, "B2": 3}, "U2":{"B1":2}},
"A2":{"U1":{"B1":4, "B2": 3}, "U2":{"B1":2}}}
How would I have to complete and correct the code posted above?
Thx, Toby