【问题标题】:Iterating through a text file to find a username and password match in Python遍历文本文件以在 Python 中查找用户名和密码匹配项
【发布时间】:2015-08-27 13:10:10
【问题描述】:

for 循环的结构看起来不对。由于某种原因,它没有正确跳转到语句的“else”部分。我将在控制台中尝试这个以简化一些事情,看看我是否有运气:

def verifylogin():
fin=open("moosebook.txt","r")
data=fin.readlines()
for line in data:
    fields=line.split()
    fields=[i.rstrip("','") for i in fields] #strips the named character from END of field
    fields=[i.replace("'",'') for i in fields]#when reading the list, you want to remoe the ',' so it isn't part of the username or password
    fields=[i.replace("(",'') for i in fields] #simiarly, remove the bracket and replace it
    line=line.rstrip()
    print(fields)

    access_permitted = False
    for counter in range(0,len(fields)):
        if textlogin.get()==fields[counter] and textpassword.get()==fields[counter+1]:
            access_permitted=True
            if access_permitted:
                    welcome=Label(myGui,text="Access Granted. Loading Profile ....")
                    welcome.pack()

        else:
                    denied=Label(myGui,text="Access Denied")
                    denied.pack()

【问题讨论】:

  • 你能展示一下fields 的样子吗?
  • 你能描述一下结果吗?
  • 您确定 textlogin.get() 和 textpassword.get() 工作正常吗?
  • 是的,textlogin 和 textpassword 肯定可以工作。我也试过这个:
  • 结果:在输入 'fields[0] 和 fields[1]' 作为用户名和密码时,它会出现“Access Granted”......然后是几个 Access Denied。当我输入下一组用户名和密码时,它会执行类似的操作,除了以一些拒绝访问开始,然后是授予访问权限。换句话说,它有点工作,但不完全......

标签: python list loops


【解决方案1】:

循环的结构方式是,文件中与您的用户名/密码不匹配的每一行都会收到“拒绝”消息,而匹配的每一行都会收到“已接受”消息。如果您只想显示一条消息,请等到循环结束再创建一条消息。

access_permitted = False
for i,s in enumerate(fields):
    if textlogin.get()==fields[i] and textpassword.get()==fields[i+1]:
        access_permitted = True

if access_permitted:
    welcome=Label(myGui,text="Access Granted. Loading Profile ....")
    welcome.pack()
else:
    denied=Label(myGui,text="Access Denied")
    denied.pack()
    line=fin.readline()

我不能肯定地说,但你似乎也可能会在循环中得到一个IndexError: list index out of range 错误,因为fields[i+1] 在最后一次迭代中超过了列表的末尾。我猜fields 是一个包含用户名+密码元组的列表,在这种情况下你应该尝试:

for username, password in fields:
    if textlogin.get()==username and textpassword.get()==password:
        access_permitted = True

如果fields 不是用户名-密码元组列表,您可能需要尝试其他方法。


如果fields 中的每个项目都包含用户名和密码以及其他项目,请尝试:

for row in fields:
    if textlogin.get()==row[0] and textpassword.get()==row[1]:
        access_permitted = True

【讨论】:

  • 我要去吃午饭,然后看看这是否有效 - 天哪,不过感谢您的快速响应!
  • 虽然我相信我已经确定了主要问题,但我认为我建议的代码不会开箱即用。 fields[i+1] 可能会给出“索引超出范围”错误。不过我还不能提出更好的建议,因为我不知道fields 是如何创建的,也不知道它的内容是什么样的。
  • 刚刚编辑添加内容:>>> ['CoderBlogJ', 'ggs123', 'J', 'Bloggs', 'Male', 'Coder)'] ['LovelyMarvJ', ' vin123', 'J', 'Marvin', '男', '可爱)']
  • 输出:12 访问被拒绝(不输入任何内容)。正确设置后(例如 CoderBlogJ 和 ggs123 用于用户名和密码),它显示“Access Granted”,然后是几个 Access Denied...
  • 你是说我的第三个代码块不起作用吗?如果没有,我认为我们真的需要查看MCVE,以便我们可以在自己的机器上复制问题。
猜你喜欢
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 2016-11-23
相关资源
最近更新 更多