Reputation: 945
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)
- What I need is that the key is in column A, and the values are in column B.
- Furthermore each row should be used instead of every second row.
- 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
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
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