How can I export my quiz results to a csv file using python 3?
Question:
I want the results to be in the format Date, Question, Name and Score
file = open("results.csv", "a")
file.write('Date, Question, Name, Scoren' + date + ',' question + ',' + name + ',' + score + 'n')
file.close()
When I run this code i keep getting the error: TypeError: Can’t convert ‘int’ object to str implicitly
Answers:
You have to cast to any ints to string string before you can concat it to another and write to file.
str(score) # <-
file.write('Date, Question, Name, Scoren' + date + ',' question + ',' + name + ',' + str(score) + 'n')
Or use str.format:
with open("results.csv", "a") as f: # with closes your files automatically
f.write('Date, Question, Name, Scoren {}, {}, {}, {}'.format(date, question, name ,score))
You may also find the csv module useful
Then convert it explicitly to str:
file.write('Date, Question, Name, Scoren' + str(date) + ',' question + ',' + name + ',' + str(score) + 'n')
I want the results to be in the format Date, Question, Name and Score
file = open("results.csv", "a")
file.write('Date, Question, Name, Scoren' + date + ',' question + ',' + name + ',' + score + 'n')
file.close()
When I run this code i keep getting the error: TypeError: Can’t convert ‘int’ object to str implicitly
You have to cast to any ints to string string before you can concat it to another and write to file.
str(score) # <-
file.write('Date, Question, Name, Scoren' + date + ',' question + ',' + name + ',' + str(score) + 'n')
Or use str.format:
with open("results.csv", "a") as f: # with closes your files automatically
f.write('Date, Question, Name, Scoren {}, {}, {}, {}'.format(date, question, name ,score))
You may also find the csv module useful
Then convert it explicitly to str:
file.write('Date, Question, Name, Scoren' + str(date) + ',' question + ',' + name + ',' + str(score) + 'n')