【发布时间】:2015-09-04 15:36:54
【问题描述】:
谁能告诉我为什么这不起作用(Python 3)以及我需要做些什么来修复它。下面的代码和错误信息:
def verifylogin():
fin=open("moosebook.txt","r")
data=fin.readlines()
line=data
allData = []
for ln in line.split(')('):
allData.append( ln.lstrip('(').rstrip(')').replace("'", '').replace(' ', '').split(',') )
for data in allData:
print (data[0]) # print user
print (data[5]) # print profession
输出错误信息:
line 30, in verifylogin
for ln in line.split(')('):
AttributeError: 'list' 对象没有属性 'split'
文本文件中的数据为:
('CoderBlogJ', 'ggs123', 'J', 'Bloggs', 'Male', 'Coder')('DoctorSmitD', 'ith123', 'D', 'Smith', 'Male', 'Doctor')('teacherminaR', 'neb123', 'R', 'minajneb', 'female', 'teacher')('WriterGardK', 'ens123', 'K', 'Gardens', 'Male', 'Writer')('', '123', '', '', '', '')('', '123', '', '', '', '')('', '123', '', '', '', '')
我希望 data[0] 和 data[5] 等打印出列表中的相关字段。
非常感谢您的回答:但是,我的最后一个任务是让用户名和密码正常工作,因为我无法翻译,所以我无法完全正常工作....
def verifylogin():
with open("moosebook.txt") as f:
data = literal_eval(f.read().replace(")", "),"))
for user, pw,_,sur,_, job in data:
if user:
print("{} is a {}".format(user, pw))
flag=0
for counter in range(0,len(data)):
if textlogin.get()==data[counter] and textpassword.get()==data[counter+1]:
flag=flag+1
if flag>0:
welcome=Label(myGui,text="Access Granted. Loading Profile ....")
welcome.pack()
else:
denied=Label(myGui,text="Access Denied")
denied.pack()
【问题讨论】:
-
行是
list,您也只是使用line = data创建对数据的引用,所以看起来多余 -
还有follow this link :)