Apr-25-2017, 02:30 PM
Quote:My dictionary contains 1 key ('AA08') and then a list of all the data [data1, data2, data3 etc..] corresponding to that 1 key.
This way and it will keep the structure in and out of csv.
Can read it in with
DictReader
.import csv d = {'AA08': ['430022', '410234', '1111'], 'AB81': ['130138', '22222', '4444']} with open("out.csv", "w") as out_file: writer = csv.writer(out_file) writer.writerow(d.keys()) writer.writerows(zip(*d.values()))
Output:AA08, AB81
430022, 130138
410234, 22222
1111, 4444
Read it back.>>> import csv ... with open('out.csv') as f: ... reader = csv.DictReader(f) ... for row in reader: ... print(row['AA08'], row['AB81']) 430022 130138 410234 22222 1111 4444Using Python 36 it will also return a
OrderedDict
.As dictionary are ordered in 36.
>>> type(row) <class 'collections.OrderedDict'> >>> row OrderedDict([('AA08', '1111'), ('AB81', '4444')])