假設我有一個像這樣的字典數據集的列表,列表寫成CSV Python的
data_set = [
{'Active rate': [0.98, 0.97, 0.96]},
{'Operating Expense': [3.104, 3.102, 3.101]}
]
我需要遍歷字典的列表,並把鑰匙作爲列標題,它的價值爲行,將其寫入CSV文件。
Active rate Operating Expense
0.98 3.104
0.97 3.102
0.96 3.101
這是我試過
data_set = [
{'Active rate': [0.98, 0.931588, 0.941192]},
{'Operating Expense': [3.104, 2.352, 2.304]}
]
import csv
with open('names.csv', 'w') as csvfile:
fieldnames = ['Active rate', 'Operating Expense']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'Active rate': 0.98, 'Operating Expense': 3.102})
writer.writerow({'Active rate': 0.97, 'Operating Expense': 3.11})
writer.writerow({'Active rate': 0.96, 'Operating Expense': 3.109})
爲了簡便起見,我已經減少了按鍵2和3
如何解決這個問題的值列表?
感謝
什麼實際問題? – thefourtheye
你想知道如何自動寫入每一行,而不是手動將這些值手動放在一起嗎? – albert
是的。我需要知道如何自動寫入每一行。 – PyAn