与我最近提出的问题类似: 我有一个文本文件包含使用这段代码的一些数据
def Add_score():
with open("users.txt") as myFile:
for num, line in enumerate(myFile, 1):
if name in line:
line_found = num
break
找到具有特定名称的行。这条线看起来像这样。
姓名:无论用户名:密码:无论得分:25分:3
我需要能够添加分数和分数
将3更改为4并将25更改为26
答案 0 :(得分:0)
你在这里:
line = 'Name: Username: password: whatever score: 25 goes: 3'
print(line)
lineSplitted = line.split()
print(lineSplitted)
updatedLine = " ".join(lineSplitted[0:5] + [str(int(lineSplitted[5])+1)] + [lineSplitted[6]] + [str(int(lineSplitted[7])+1)])
print(updatedLine)
打印:
Name: Username: password: whatever score: 25 goes: 3
['Name:', 'Username:', 'password:', 'whatever', 'score:', '25', 'goes:', '3']
Name: Username: password: whatever score: 26 goes: 4