I have a Python Dictionary that consists of keys an value pairs where each values is composed of a list. The lists vary in length. The dictionary has the following format:
diseaseDict = {'NCT01266330': ['Oxidative Stress', 'Inflammation'],
'NCT01266343': ['Glaucoma', 'Acute Primary Angle-closure Glaucoma'],
'NCT01266356': ['Traumatic Brain Injury'],
'NCT01266369': ['Mastocytosis'],
'NCT01266382': ['Osteoarthritis', 'Spinal Diseases', 'Ligament Rupture', 'Lower Extremity Fracture', 'Neurological Disorders'],
'NCT01266395': ['COPD']}
I need to write it to a CSV file in the long format. I've tried a couple of iterations but couldn't get it just right. How do I write Python Dictionaries like this to a CSV using the long data format ?
This is what the Python dictionary with its keys and values (from lists) would look in the long data format:
import csv
header = ['key', 'valuesList']
with open('file.csv', 'w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
for key, value in diseaseDict.items():
for i in value:
writer.writerow([key, i])