I'm trying to write a nested dictionary to a .csv file. Here is is a simple example:
import csv
import itertools
fields = [ 'org', '2015', '2014', '2013' ]
dw = { 'orgname1': { '2015' : 2, '2014' : 1, '2013' : 1 },
'orgname2': { '2015' : 1, '2014' : 2, '2013' : 3 },
'orgname3': { '2015' : 1, '2014' : 3, '2013' : 1 }
}
with open("test_output.csv", "wb") as f:
w = csv.writer( f )
years = dw.values()[0].keys()
for key in dw.keys():
w.writerow([key, [dw[key][year] for year in years]])
This gets me a table with two columns: the first contains orgname
; the second contains [2, 1, 1] (or the corresponding values from the sub-dictionary). I'd like a table with four columns: one for orgname
and then three for the corresponding list elements.