我有以下代码,其中的注释解释了输出和所需的输出。我似乎无法访问(或理解逻辑)如何访问列表中的不同字段。
def viewrecs(username):
username = (username + ".txt")
with open(username,"r") as f:
fReader=csv.reader(f)
for row in fReader:
for field in row:
#print(field) #this prints everything in the file
#print(row) #this also prints everything in the file
#print(row[0]) #this also prints everything!!!!!!
#print(field[0]) #this prints the first ELEMENT in each row, namely the "["
#How do I access the first field of each column (the genre, like SCI-FI, or Romance)
有问题的文件是基于用户用户名的文本文件。内容如下:
"['Sci-Fi', 'Blade Runner']"
"['Sci-Fi', 'Star Trek']"
"['Sci-Fi', 'Solaris']"
"['Sci-Fi', 'Cosmos']"
"['Drama', ' The English Patient']"
"['Sci-Fi', 'Out of the Silent Planet']"
"['Drama', ' The Pursuit of Happiness']"
"['Drama', ' The English Patient']"
"['Drama', ' Benhur']"
"['Drama', ' Benhur']"
关于如何访问列字段(例如科幻,戏剧等)的答案以及解释将非常感谢。
我想打印整个第一列......即SCI-FI,SCI-FI,SCI-FI,DRAMA等......我需要将它们理想地读入列表以便它们可以被操作在列表中
根据以下答案之一,如果问题是关于数据首先写入文件的方式,则将此数据写入文件的代码/函数如下:
def viewfilmfunction(x,username):
#open the file as student file (variable)
print(username, ":You are about to view Film:", x, "Enter the selection ID number of the film again to confirm viewing")
with open("films.txt","r") as filmsfile:
#prompt the user to enter the ID number they require
idnumber=input("Enter the ID number you require:")
#call upon our reader (this allows us to work with our file)
filmsfileReader=csv.reader(filmsfile)
#for each row that is read by the Reader
for row in filmsfileReader:
#and for each field in that row (this does it automatically for us)
for field in row:
#if the field is equal to the id number that is being searched for
if field ==idnumber:
#print the row fields (genre and title) corresponding to that ID number
#create a list which contains the relevant fields in the row.
viewedlist=[row[1],row[2]]
print("You have viewed:", viewedlist)
with open("fakeflixfile.txt","r")as membersfile:
#Open Reader
membersfileReader=csv.reader(membersfile)
for row in membersfileReader:
for field in row:
if field==username:
#Open Writer to append to file -this time it looks for the file stored by that username
with open("%s.txt" % username,"a") as membersfile:
membersfileWriter=csv.writer(membersfile)
#Use the writer to append the viewedlist to the appropriate member's user file.
membersfileWriter.writerow([viewedlist])
FAKEFLIXFILE.txt内容
username13022,Username@123,user,name1,2/02/3022,user Road,US455P,Mle,Nothing,username1@hotmail.com
AugustineSalins1900.txt,Augstine@123,Augustine,Salins,5/02/1900,Hutchins Road,CRBBAAA,Male,Theology,augustine@hotmail.com
JosieBletchway3333,Bb@123,Josie,Bletchway,29/02/3333,Bletch Park,BB44AA,Female,Encryption,blecth@hotmail.com
JoeBloggs.0.0,Joe@Bloggs12,Joe,Bloggs,0.0.0.0,0 Road,00000,Male,Joe Blogs,joe@hotmail.com
更新: 我现在更改了txt文件(基于用户名),以便生成的输出不会生成列表:
Drama, The English Patient
Drama, Benhur
Sci-Fi,Cosmos
然而,在跑步时:
def viewrecs(username):
#pass
username = (username + ".txt")
with open(username,"r") as f:
fReader=csv.reader(f)
for row in fReader:
print(eval(row)[0])
错误仍然存在:
print(eval(row)[0])
TypeError: eval() arg 1 must be a string, bytes or code object
答案 0 :(得分:1)
因为您的文件不是有效的CSV文件。它看起来更像是一系列JSON对象。文件中的每一行都用双引号括起来。因此,CSV阅读器将其视为单个列。这就是你在行[0]
中得到的原因这是由您编写文件的方式引起的。下面的行告诉CSV编写器写一个对象,该对象是包含两个值的列表
membersfileWriter.writerow([viewedlist])
你真正想要的是告诉CSV编写器写多个对象。将您的行更改为此行,无需将其括在方括号中:
membersfileWriter.writerow(viewedlist)
然后您可以使用它来阅读您的文件:
with open(username,"r") as f:
fReader=csv.reader(f)
for row in fReader:
print (row) # print entire row
print (row[0]) # print just the first field