Prasath
(Prasath)
August 11, 2021, 4:58pm
1
I am having a dictonary with one key and multiple value combinations as shown below:
{‘Region’: ([US, UK]), ‘Time’: ([9, 10]), ‘ISO’: ([21, 22]), ‘ISO1’: ([1, 3])}
I need to convert it into a CSV file as shown below.
Region,Time,ISO,ISO1
US,9,21,1
UK,10,22,3
I have tried many options. Need to do it with csv writer without using Pandas.
BowlOfRed
(BowlOfRed)
August 11, 2021, 8:17pm
2
The dictionary you show doesn’t have proper quoting around the strings inside. I’ve added those quotes. But otherwise, you can zip the elements from each value together and pass them to csvwriter.
import csv
import sys
yourdict = {"Region": (["US", "UK"]), "Time": ([9, 10]), "ISO": ([21, 22]), "ISO1": ([1, 3])}
writer = csv.writer(sys.stdout)
writer.writerow(yourdict.keys())
writer.writerows(zip(*yourdict.values()))
Region,Time,ISO,ISO1
US,9,21,1
UK,10,22,3