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:
For posterity:
You should use iteritems() to iterate over a dictionary, so the last part becomes
for name, values in itemDict.iteritems():
print values
listWriter.writerow(values)
Easiest Way
You can convert the dictionary into Dataframe and write it to csv Eg
import pandas as pd
my_dict = {"tester": 1, "testers": 2}
df=pd.DataFrame(my_dict,index=[0])
df.to_csv("path and name of your csv.csv")
output
tester testers
0 1 2
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.
d = [{'a': 1, 'b': 2},{'a': 3, 'b': 4}]
with open('csv_file.csv', 'w', newline='\n') as f:
w = csv.DictWriter(f, d[0].keys())
w.writeheader()
for i in d:
w.writerow(i)
gets you
a,b
1,2
3,4
Sample data:
mydict = [{"col1": 1000, "col2": 2000}, {"col1": 3000, "col2": 4000}]
One-liner for converting a list of dicts to CSV, using pandas:
import pandas as pd
pd.DataFrame(mydict).to_csv('out.csv', index=False)
Results:
col1,col2
1000,2000
3000,4000
This is what i use, its simple and works fine for me. when you have only one dictionary, use this
my_dict = {"tester": 1, "testers": 2}
with open('mycsvfile.csv', 'wb') as f:
w = csv.DictWriter(f, my_dict.keys())
w.writerow(dict((fn,fn) for fn in my_dict.keys()))
w.writerow(my_dict)
$ cat mycsvfile.csv
testers,tester
2,1
When you have a list of dictionaries, like what you get from SQL queries, you do like this.
my_dict = ({"tester": 1, "testers": 2},{"tester": 14, "testers": 28})
with open('mycsvfile.csv', 'wb') as f:
w = csv.DictWriter(f, my_dict[0].keys())
w.writerow(dict((fn,fn) for fn in my_dict[0].keys()))
w.writerows(my_dict)
cat mycsvfile.csv
testers,tester
2,1
28,14