简体   繁体   中英

python: dictionary to .csv-file

I am trying to write a dictionary to a csv file in python. 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:

在此处输入图片说明

My python code:

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.
  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]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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