我想将数据写入CSV文件,但我无法让代码写入任何变量。我正在运行大量游戏,我希望将所有数据都写入CSV文件,以显示游戏编号和结果(赢/输/平局)。
def function():
#gets game result
file = open("data.csv", "w")
for i in range(10):
#function()
file.write("game i", result)
file.close()
我想要的是:
game 1, result
game 2, result
game n, result
1条答案
按热度按时间2eafrhcq1#
使用将扩展变量内容的格式字符串:
虽然我会使用
csv
模块,因为它可以正确处理带有分隔符和/或引号的数据列:正确使用逗号将结果引起来并转义嵌入引号的输出:
要读取和解析数据: