I am trying to write multiple dictionaries to a csv file, where header (key) gets written only once and rows (values) are written based on the key. I was able to figure this out for two dictionaries, but what if I am getting multiple dictionaries that need to be written?
I am streaming tweets which get converted to json, so I am trying to end with a CSV file that is sorted by each JSON key. Here is a more detailed explanation of what I am trying to do (Writing multiple JSON to CSV in Python - Dictionary to CSV Here is what I am trying to end up with but with thousands of potential rows of data (preferably sorted by key if possible):
Here is my basic code for two dictionaries:
import csv
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4', 'key5': 'value5', 'key6': 'value6', 'key7': 'value7'}
my_dict2 = {'key1': 'value1A', 'key2': 'value2A', 'key3': 'value3A', 'key4': 'value4A', 'key5': 'value5A', 'key6': 'value6A', 'key7': 'value7A'}
with open('mycsvfile.csv', 'wb') as f:
w = csv.DictWriter(f, my_dict.keys())
w.writeheader()
w.writerow(my_dict)
if my_dict.keys() == my_dict2.keys():
w.writerow(my_dict2)
print my_dict
P.S. I am a begginer!