How can I export my quiz results to a csv file using python 3?
本问题已经有最佳答案,请猛点这里访问。
我希望结果的格式是日期、问题、姓名和分数
1 2 3 4 5 | file = open("results.csv","a") file.write('Date, Question, Name, Score ' + date + ',' question + ',' + name + ',' + score + ' ') file.close() |
当我运行此代码时,不断得到错误:typeerror:无法将"int"对象隐式转换为str
然后将其显式转换为str:
1 2 3 | file.write('Date, Question, Name, Score ' + str(date) + ',' question + ',' + name + ',' + str(score) + ' ') |
您必须先将任何int转换为字符串,然后才能将其转换为另一个int并写入文件。
1 2 3 4 5 | str(score) # <- file.write('Date, Question, Name, Score ' + date + ',' question + ',' + name + ',' + str(score) + ' ') |
或使用str.format:
1 2 3 | with open("results.csv","a") as f: # with closes your files automatically f.write('Date, Question, Name, Score {}, {}, {}, {}'.format(date, question, name ,score)) |
号
您还可以发现csv模块很有用