【发布时间】:2018-09-30 22:38:40
【问题描述】:
我有这个功能:
def favourites():
name = input("Enter your name as you did when you signed up: ")
new_artist = input("Would you like to edit your favourite artist? y/n ").lower().strip()
if new_artist == 'yes' or new_artist == 'y':
old_artist = input("Enter the favourite artist you used when signing up: ")
artist = input("Enter your new favourite artist: ")
usersFile = open('users.txt', 'r+')
usersRec = usersFile.readline()
# reads each line in the file
while usersRec != "":
# splits each record into fields
field = usersRec.split(',')
if field[0] == name:
usersFile.write(field[2].replace(old_artist, artist))
usersRec = usersFile.readline()
usersFile.close()
我已读取文本文件中的一行,然后将其拆分为多个字段,我想更新一个字段。搜索并找到了 update() 函数,所以尝试了一下,但它不起作用,我不确定我做错了什么。有什么建议吗?
【问题讨论】:
标签: python python-3.x file-handling