Finn Eggers
Finn Eggers

Reputation: 945

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:

enter image description here

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

Upvotes: 0

Views: 55

Answers (2)

shayelk
shayelk

Reputation: 1646

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

Upvotes: 0

Rakesh
Rakesh

Reputation: 82755

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 

Upvotes: 1

Related Questions