我嘗試了可以寫入的每種格式。每當Python到達要追加到Scores.csv
的行時,它就會覆蓋該文件。「a」覆蓋文件而不是追加
有沒有錯誤,它只是不會追加。我不確定是否因爲我正在嘗試寫入.csv
文件而不是.txt
,但是如果是因爲這個原因,則必須有一種解決方法。
下面是錯誤代碼:
#OPEN TEMP_SCORES FILE FOR READING
with open("temp_Scores.csv", "r") as scorefile:
print("\nDEBUG: temp_Scores.csv is open.")
reader = csv.DictReader(scorefile)
for row in reader:
print("\nDEBUG: Reading temp_Scores.csv..")
with open("Scores.csv", "w") as scorefile:
print("\nDEBUG: Scores.csv is open for writing...")
fieldnames = ["Name", "Class", "Score 1", "Score 2", "Score 3"]
writer = csv.DictWriter(scorefile, fieldnames=fieldnames)
#This line writes the actual "header" or "titles"
writer.writeheader()
NameExists = False
#LOGIC TO DETERMINE WHAT TO WRITE
#if new user's name is the same as in the file;
if row["Name"] == UserData["Name"]:
NameExists = True
print("\nDEBUG: Name was found in temp_Scores.csv. Overwriting line..")
#if Score 1 doesn't exist;
if len(row["Score 1"]) == 0:
#makes NEW SCORE 1 the user's score
row["Score 1"] = UserData["Score"]
#actually write new scores to file
writer.writerow({"Name": row["Name"], "Class": UserData["Class"], "Score 1": row["Score 1"], "Score 2": row["Score 2"], "Score 3": row["Score 3"]})
#if Score 2 doesn't exist;
elif len(row["Score 2"]) == 0:
#makes NEW SCORE 2 the user's score
row["Score 2"] = UserData["Score"]
#actually write new scores to file
writer.writerow({"Name": row["Name"], "Class": UserData["Class"], "Score 1": row["Score 1"], "Score 2": row["Score 2"], "Score 3": row["Score 3"]})
#if Score 3 doesn't exist;
elif len(row["Score 3"]) == 0:
#makes NEW SCORE 3 the user's score
row["Score 3"] = UserData["Score"]
#actually write new scores to file
writer.writerow({"Name": row["Name"], "Class": UserData["Class"], "Score 1": row["Score 1"], "Score 2": row["Score 2"], "Score 3": row["Score 3"]})
#if all Scores exist already; (update scores)
else:
#makes NEW SCORE 3 the user's current score
newScore3 = UserData["Score"]
#makes NEW SCORE 2 the user's OLD SCORE 3
newScore2 = row["Score 3"]
#makes NEW SCORE 1 the user's OLD SCORE 2
newScore1 = row["Score 2"]
#gets rid of OLD SCORE 1
#actually write new scores to file
writer.writerow({"Name": row["Name"], "Class": UserData["Class"], "Score 1": newScore1, "Score 2": newScore2, "Score 3": newScore3})
print("\nDEBUG: Updated new user's line")
#if new user's name DOESN'T exist in the file already;
if NameExists == False:
print("\nDEBUG: Name was not found in temp_Score.csv. Appending new line..")
#saves new user's details into a NEW LINE in the ORIGINAL FILE
with open("Scores.csv", "a") as scorefile:
fieldnames = ["Name", "Class", "Score 1", "Score 2", "Score 3"]
writer = csv.DictWriter(scorefile, fieldnames=fieldnames)
writer.write({"Name": UserData["Name"], "Class": UserData["Class"], "Score 1": UserData["Score"], "Score 2": "", "Score 3": ""})
print("\nDEBUG: Appended new line onto existing file")
#temp_Scores.csv is no longer needed, so we're getting rid of it
os.remove("temp_Scores.csv")
print("\nDEBUG: Deleted temp_Scores.csv.")
非常感謝提前任何幫助。
你確定問題不是'打開(「Scores.csv」,「w」)作爲scorefile'位? – user2357112