简体   繁体   English

python:字典到.csv文件

[英]python: dictionary to .csv-file

I am trying to write a dictionary to a csv file in python. 我正在尝试在python中将字典写入csv文件。 I've tried nearly every tutorial that I could find but did not get a good result yet. 我已经尝试了几乎所有可以找到的教程,但都没有得到很好的结果。 I want to later edit that file in excel but it looks like this right now: 我想稍后在excel中编辑该文件,但现在看起来像这样:

在此处输入图片说明

My python code: 我的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)
  1. What I need is that the key is in column A, and the values are in column B. 我需要的是键在A列中,值在B列中。
  2. Furthermore each row should be used instead of every second row. 此外,应该使用每一行而不是第二行。
  3. It would be nice to remove the "," between the key and the value. 最好删除键和值之间的“,”。

I am very happy if someone could help me with this. 如果有人可以帮助我,我感到非常高兴。

Greetings, Finn 芬恩,问候

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 

you can go with 你可以去

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM