【发布时间】:2020-04-14 22:55:56
【问题描述】:
我有一个包含名称和结果的文本文件。如果名称已经存在,则只应更新结果。我尝试使用此代码和许多其他代码,但没有成功。
文本文件的内容如下所示:
Ann, 200
Buddy, 10
Mark, 180
Luis, 100
PS:我是 2 周前开始的,所以不要评判我的代码不好。
from os import rename
def updatescore(username, score):
file = open("mynewscores.txt", "r")
new_file = open("mynewscores2.txt", "w")
for line in file:
if username in line:
splitted = line.split(",")
splitted[1] = score
joined = "".join(splitted)
new_file.write(joined)
new_file.write(line)
file.close()
new_file.close()
maks = updatescore("Buddy", "200")
print(maks)
【问题讨论】:
-
但没有成功。你能说得更具体些吗?代码有什么问题?您也没有清楚地解释程序要做什么,这意味着我们都必须尝试从您的代码中猜测。
标签: python python-3.x function