在Python中更新文件的内容

我正在尝试更新文件swimmerpoints.txt的内容。我当前的代码如下:

def update():
  swimmerid = []
  name = []
  points = []
  files = open('swimmerpoints.txt','a+')
  for info in files:
    info = info.strip()
    lists = info.split(',')
    swimmerid.append(lists[0].strip())
    name.append(lists[1].strip())
    points.append(lists[2].strip())

  x = int(input("Swimmer ID to update points: "))
  index = swimmerid.index(x)
  print(name[index] + "'s","point total is:",points[index])
  changes = int(input("Enter the new point value: "))
  files.write(f"{changes}")
  files.close()
update()

swimmerpoints.txt的内容如下:1,JOHNNY,492。当我提示输入游泳者ID(在本例中为1)时,出现此错误:

Traceback (most recent call last):
  File "main.py",line 29,in <module>
    update()
  File "main.py",line 24,in update
    index = swimmerid.index(x)
ValueError: 1 is not in list

1位于swimmerid

的位置

JOHNNY在name的位置

492位于points

的位置

读取文件并与正确的ID匹配的正确方法是什么?

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...