There have been a bunch of permutations of my question but the closest answer:
How do I write a Python dictionary to a csv file?
unfortunately printed output on each row like so:
matthew, green
rachel, blue
raymond, red
where somedict = dict(raymond = "red", rachel = "blue", matthew = "green")
and my problem requires output more like this:
why, date, story
"because", "sunday", "blah blah blah"
I'm getting the dictionary from user input, and it's important that the output file places each new answer to (why, date and story) on a new line (and furthermore, that column headers are only printed once). So my code looks like this:
import csv
o = "file.csv"
why = input("Why? ")
date = input("Date: ")
story = input("Story: ")
And in summary, I need output that looks like this:
why, date, story
"because", "sunday", "blah blah blah"
"i don't know", "monday", "blah blah blue"
"third base", "monday", "blah blah blarg"
I was just looking for a simple way to take user input, write it to a new line in a csv file. I've been at this for about two hours now, so that's why I'm here. First I got two TypeError errors: (1) 'str' does not support the buffer interface (which I solved by adding the "utf-8" parameter to the with open(file, "wb", "utf-8") statement). (2) TypeError: an integer is required. For some reason I took the "b" out of "wb" and it seemed to get rid of this error. The link I posted to gets me almost to where I need to be, but not quite enough and I would sincerely appreciate any quick clarification anyone can provide.
EDIT
TO be clear, I tried a few possibilities. The most promising were:
with open("file.csv", "w", encoding = "utf-8") as f:
w = csv.writer(f)
w.writerows(somedict.items())
which produced the first output I cited above, which isn't what I need.
I also tried:
with open("file.csv", "w", encoding = "utf-8") as f:
w = csv.writer(f)
w.writerow(somedict.keys())
w.writerow(somedict.values())
But this gave me: _csv.Error: sequence expected