2015-11-01 19 views
0

我嘗試寫入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) 
for z in toPrint: 
    highscores.write(z) 
highscores.close() 
+0

您認爲「toPrint =(name +」,「+ score +」\ n「)'在做什麼? – IanAuld

+0

http://stackoverflow.com/a/2363742/5203702 –

回答

0

您需要在「追加」模式,而不是「寫」來打開該文件。您的代碼將保持不變,只有以下行會發生變化:

highscores=open("Highscores.csv","a") 
相關問題