Python 当我写入CSV文件时,存储的数据将被删除并替换为新数据

Python 当我写入CSV文件时,存储的数据将被删除并替换为新数据,python,csv,Python,Csv,我尝试写入CSV文件,但当我检查该文件时,仅显示最后一组数据?如何解决这个问题? 我的原始代码是: highscores=open("Highscores.csv","w") toPrint=(name+","+score+"\n") for z in toPrint: highscores.write(z) highscores.close() 我也试过: toPrint=[] output=(name+","+score+"\n") toPrint.append(output) f

我尝试写入CSV文件,但当我检查该文件时,仅显示最后一组数据?如何解决这个问题? 我的原始代码是:

highscores=open("Highscores.csv","w")
toPrint=(name+","+score+"\n")
for z in toPrint:
    highscores.write(z)
highscores.close()
我也试过:

toPrint=[]
output=(name+","+score+"\n")
toPrint.append(output)
for z in toPrint:
    highscores.write(z)
 highscores.close()

您需要以“附加”模式而不是“写入”模式打开文件。您的代码将保持不变,但以下行将更改:

highscores=open("Highscores.csv","a")

你认为这个
toPrint=(name+“,“+score+”\n”)
在做什么?