[英]python: dictionary to .csv-file
我正在嘗試在python中將字典寫入csv文件。 我已經嘗試了幾乎所有可以找到的教程,但都沒有得到很好的結果。 我想稍后在excel中編輯該文件,但現在看起來像這樣:
我的python代碼:
s = file_name
dict = App.get_running_app().get_widget_contents()
with open(s, 'w') as f:
writer = csv.writer(f)
for row in dict.items():
writer.writerow(row)
- 我需要的是鍵在A列中,值在B列中。
- 此外,應該使用每一行而不是第二行。
- 最好刪除鍵和值之間的“,”。
如果有人可以幫助我,我感到非常高興。
芬恩,問候
s = file_name
dict = App.get_running_app().get_widget_contents()
with open(s, 'w') as f:
writer = csv.writer(f)
writer.writerow(dict.keys()) #Header
for row, value in dict.items():
writer.writerow(value) #Write Values
你可以去
s = file_name
dict = App.get_running_app().get_widget_contents()
with open(s, 'w') as f:
writer = csv.writer(f)
for key in dict:
writer.writerow([key, dict[key]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.