[英]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.