I have written code to read a CSV into a python dictionary, which works fine. I\'m trying to get the dictionary back to a CSV. I have written the following:
The default writer expects a list, which is why it won't work for you. To use the dictwriter, just change your listwriter =
line to this:
with open('/Users/broberts/Desktop/Sum_CSP1_output.csv', 'wb') as outfile:
listWriter = csv.DictWriter(
outfile,
fieldnames=itemDict[itemDict.keys()[0]].keys(),
delimiter=',',
quotechar='|',
quoting=csv.QUOTE_MINIMAL
)
Or, you can just set fieldnames
to be fieldnames=['arbitrary','list','of','keys']
if you know what the fields are supposed to be.