I have a list that I could use some help writing to a csv file.
In one column - I need the category. This is the field in single quotes right before the colon. After that comes each value within the immediate set of brackets - each in it's own column:
Below is a snapshot of the print out of the list itself...
defaultdict(<class 'list'>, {
'Analyzers/Blood': ['Analyzers, Laboratory, Hematology, Coagulation', 'Analyzers, Laboratory, Hematology, Coagulation, Semiautomated', 'Flowmeters, Blood, Ultrasonic'],
'LCD Display': ['Auxiliary Displays, Touchscreen'],
'Electronic Endoflator': ['Balances, Electronic', 'Carts, Storage/Transport, Electromechanical/Electronic Instrument/Equipment', 'Thermometer, Electronic, Traceable']
})
Format example:
Analyzers/Blood | Analyzers | Laboratory | Hematology...(through Ultrasonic)
thanks in advance
updated I missed your lists could have multiple entries.
Try this:
dict_ = {
'Analyzers/Blood': ['Analyzers, Laboratory, Hematology, Coagulation', 'Analyzers, Laboratory, Hematology, Coagulation, Semiautomated', 'Flowmeters, Blood, Ultrasonic'],
'LCD Display': ['Auxiliary Displays, Touchscreen'],
'Electronic Endoflator': ['Balances, Electronic', 'Carts, Storage/Transport, Electromechanical/Electronic Instrument/Equipment', 'Thermometer, Electronic, Traceable']
}
with open("output.csv","w") as f:
for k,v in dict_.items(): #py2 iteritems()
items = [item.strip() for subv in v for item in subv.split(",")]
f.write('|'.join([k]+list(set(items))))
f.write('\n')
Output:
Analyzers/Blood|Blood|Ultrasonic|Coagulation|Flowmeters|Laboratory|Hematology|Analyzers|Semiautomated
LCD Display|Auxiliary Displays|Touchscreen
Electronic Endoflator|Storage/Transport|Traceable|Thermometer|Balances|Electromechanical/Electronic Instrument/Equipment|Carts|Electronic