Write dictionary (keys and values) to a csv file
dictionary, python, python-2.7
Solution
import csv
dict_data = {'key1': [1, 2, 3], 'key2': [4, 5, 6]}
with open("dict2csv.txt", 'w') as outfile:
csv_writer = csv.writer(outfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for k,v in dict_data.items():
csv_writer.writerow([k] + v)
This code will write each key, value pair in your desire format on separate line in csv file.
Problem
I just want the csv file to look like this: key,item1,item2,item3 key2,itema,itemB,itemC and so on The dictionary has a key and the value is a list of floats. This is the current code I have to write to the csv file but all it does is write out the key like this: k,e,y,s Any help is appreciated ``` with open(outFileName1, 'w') as outfile: csv_Writer = csv.writer(outfile) csv_Writer.writerows(dict1) ```