从具有特定列分配的字典中写入 CSV

Write CSV from dictionary with specific column assignments

我有一个列表,我可以使用一些帮助写入 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)

提前致谢

已更新我错过了你的列表可能有多个条目。

试试这个:

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