我有一个列表,我可以使用一些帮助写入csv文件。
在一栏中 - 我需要这个类别。这是冒号前面的单引号字段。在此之后,直接括号中的每个值都出现在 - 它们自己的列中:
下面是列表本身打印的快照......
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']
})
格式示例:
Analyzers/Blood | Analyzers | Laboratory | Hematology...(through Ultrasonic)
提前致谢
答案 0 :(得分:1)
已更新我错过了您的列表可能有多个条目。
试试这个:
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')
输出:
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