I am trying to read from a csv file and store the data in a nested dictionary.
CSV file content
Type, ID, Frequency
Advanced,AAA,30 secs
Advanced,AAA,60 secs
Advanced,BBB,30 secs
Basic,CCC,30 secs
Basic,CCC,60 secs
Basic,DDD,30 secs
Expected output where the 'type' is the higher level key with the values as another dictionary with the ID and frequency as the key/value pair.
{'Advanced': {'AAA':['30 secs', '60 secs'], 'BBB':['30 secs']}, 'Basic': {'CCC':['30 secs', '60 secs'], 'DDD':['30 secs']}}
With two columns I got it to work using a defaultdict container.
symbols = co.defaultdict(list)
with open(filename, 'r') as f:
lines = csv.DictReader(f)
for line in lines:
print(line)
symbols[line['Type']].append(line['ID'])