将更改的值从列表写入文件,第二次迭代不起作用,Python



下面的函数是一个更大程序的一部分,它试图允许用户"喜欢"一部电影,并将该值(通过递增)存储在电影文本文件中。奇怪的是,它在第一次迭代中工作正常(将其读取到列表中,更改值,写入文件),但在第二次迭代中,出现了错误。

该文件名为"films.txt",内容为:

0,Genre,Title,Rating,Likes
1,Sci-Fi,Out of the Silent Planet,PG,0
2,Sci-Fi,Solaris,PG,0
3,Sci-Fi,Star Trek,PG,0
4,Sci-Fi,Cosmos,PG,0
5,Drama,The English Patient,15,1
6,Drama,Benhur,PG,0
7,Drama,The Pursuit of Happiness,12,0
8,Drama,The Thin Red Line,18,0
9,Romance,When Harry met Sally,12,0
10,Romance,You've got mail,12,0
11,Romance,Last Tango in Paris,18,0
12,Romance,Casablanca,12,0

此特定功能的代码如下:注意:idnumber指的是每部电影的idnumber。例如,《星际迷航》的ID号是3。

def likeafilm(x,username):
#prompt the user to enter the ID number they require
idnumber=int(input("To confirm, please enter the number of the film you wish to like:"))
#create a list which stores and displays all the data in the films txt file
with open("films.txt",mode="r", encoding="utf8") as f:
allfilms=[]
reader=csv.reader(f)
for row in reader:
allfilms.append([element.strip() for element in row])
#print(allfilms)
print("print the third film", allfilms[3])
print("print the second film", allfilms[2])
print("print the current film selected", allfilms[idnumber])
print("print the like for current film", allfilms[idnumber][4])
allfilms[idnumber][4]=str(int(allfilms[3][4])+1) #this succesfully increments the record in the runtime list by 1
print(allfilms) #this confirms the update - this now needs to be written to file.
with open("films.txt","w") as f:
writer=csv.writer(f)
writer.writerows(allfilms)
print("******BACK TO VIEWING FILMS SELECTION***********")
watchfilms(username)

在第一次迭代,或者我第一次运行程序时,它运行得很好!1是递增的(对于点赞),一切都很好。第二次出现的错误如下。。。。

print("print the like for current film", allfilms[idnumber][4])
IndexError: list index out of range

更新代码

def likeafilm(x,username):
#prompt the user to enter the ID number they require
idnumber=int(input("To confirm, please enter the number of the film you wish to like:"))
#create a list which stores and displays all the data in the films txt file
with open("films.txt",mode="r", encoding="utf8") as f:
allfilms=[]
reader=csv.reader(f)
for row in reader:
allfilms.append([element.strip() for element in row])
#print(allfilms)
print("print the third film", allfilms[3])
print("print the second film", allfilms[2])
print("print the current film selected", allfilms[idnumber])
print("print the like for current film", allfilms[idnumber][4])
allfilms[idnumber][4]=str(int(allfilms[3][4])+1) #this succesfully increments the record in the runtime list by 1
print(allfilms) #this confirms the update - this now needs to be written to file.
with open("films.txt","w") as f:
writer=csv.writer(f)
#writer.writerows(allfilms)
allfilms.append([element.strip() for element in row])
writer.writerows(allfilms)
print("******BACK TO VIEWING FILMS SELECTION***********")
watchfilms(username)    

正如Apollo在下面建议的那样,我可以看到发生了什么(错误),但不知道如何修复。它在第一次迭代中有效,因为文件是正确的(没有空格)。然而,在第二次迭代中,文件已被覆盖,并且它似乎对文件的内容做了一些操作。第二次出现这个错误:

print("print the like for current film", allfilms[idnumber][4])
IndexError: list index out of range

我在《电影5》(顺便说一句,《英国病人》是一部很棒的电影)中尝试过这个,虽然它第一次更新了它,点赞数增加到了"1",但第二次没有。

我还尝试将文件写入程序更改为"wb"而不是"w",但也出现了以下错误:

writer.writerows(records)
TypeError: 'str' does not support the buffer interface

您的想法是正确的,但我会将其分解为两个函数,一个用于增加电影般的效果,另一个用于用户交互。此外,您试图在对内置函数open()的调用中指定"encoding",这不是一个有效的参数。

import csv
films_file = "films.txt"
def like_film(x):
with open(films_file,mode="r") as f:
reader = csv.reader(f)
records = [row for row in reader]
for rec in records:
if int(rec[0]) == x:
rec[-1] = str(int(rec[-1]) + 1)
with open("films.txt","wb") as f:
writer=csv.writer(f)
writer.writerows(records)
def user_like_film(username):
x = int(input("Enter the ID of the film you wish to like: "))
like_film(x)
watchfilms(username)

找到答案:

在Python 2中,文件需要使用"wb"而不是"w"打开,正如您所建议的那样。

然而,我正在使用Python 3。在Python 3中,所需的语法略有不同。需要使用附加参数打开该文件,即:newline=''。

以下示例:

# Python 2
with open('filmsfile.txt', 'wb') as f:
writer = csv.writer(f)
# Python 3
with open('filmsfile.txt', 'w', newline='') as f:
writer = csv.writer(f)

相关内容

最新更新