简体   繁体   English

从文本文件检查字典键值

[英]Checking Dictionary Key Value from Text File

Python beginner here and I'm trying to create a login system (using Python 3).这里是 Python 初学者,我正在尝试创建一个登录系统(使用 Python 3)。 I've created a function to store user's username and password as a dictionary {uname:pswd} in a text file.我创建了一个函数来将用户的用户名和密码作为字典 {uname:pswd} 存储在文本文件中。 I created another function that lets the user log in but I don't know how to check if their details match the dictionary keys and its corresponding values in the text file.我创建了另一个允许用户登录的函数,但我不知道如何检查他们的详细信息是否与文本文件中的字典键及其相应值匹配。

This is what I've done so far:这是我到目前为止所做的:

def user_login(): #login function
    uname=input("Enter username:")
    pswd=input("Enter password:")
    d={uname:pswd}
    loginfile=open('logindata.txt', 'r')
    with loginfile as f:
        for line in f:
            if d.get(uname)==pswd: #should check user input matches dictionary in text file
                return "Accepted"
            else:
                return "Wrong username/password"
    loginfile.close()

With this, I get an error message: AttributeError: 'str' object has no attribute 'get' .有了这个,我收到一条错误消息: AttributeError: 'str' object has no attribute 'get' It reads it as a string, so how do I get the code to read it as a dictionary?它将它作为一个字符串读取,那么我如何获得将它作为字典读取的代码? Also, as a newbie, I'm pretty sure some parts of my code could be wrong.另外,作为新手,我很确定我的代码的某些部分可能是错误的。 Any suggestions on what I should fix?关于我应该修复什么的任何建议?

I execute your same code and got no error, but no matter what you typed as credentials, they always were accepted.我执行您的相同代码并且没有出错,但是无论您输入什么作为凭据,它们总是被接受。 Here is a way to solve your problem这是解决您问题的方法

def user_login():  # login function
    uname = input("Enter username:")
    pswd = input("Enter password:")
    d = {uname: pswd}

    with open('logindata.txt', 'r') as f: # here I open logindata.txt in read mode and I call it f
        credentials = f.read().split('\n') # I make a list with the credentials by splitting the lines of the file (I think your file has 2 lines: username and password)
        check_dict = {credentials[0]: credentials[1]} # I create the dictionary with the credentials

    if d.get(uname) == check_dict.get(credentials[0]):  # check if the input username and password (d dictionary) are the same as those stored in the logindata.txt file
        return "Accepted"
    else:
        return "Wrong username/password"

the problem is that your code checks if the value of uname in the d dictionary is equal to pswd , but when you declared the dictionary d={uname:pswd} you are assigning the value of pswd to uname , so they will always be the same.问题是您的代码检查d字典中uname的值是否等于pswd ,但是当您声明字典d={uname:pswd}您将pswd的值分配给uname ,因此它们将始终是相同的。 I also used with open('logindata.txt', 'r') as f: instead of loginfile=open('logindata.txt', 'r') with loginfile as f: ... loginfile.close() because is just a better way to open a file and you have to type less code.我还使用with open('logindata.txt', 'r') as f:而不是loginfile=open('logindata.txt', 'r') with loginfile as f: ... loginfile.close()因为是只是一种打开文件的更好方法,而且您必须键入更少的代码。 Anyway I would suggest not using dictionaries and simply doing this无论如何,我建议不要使用字典而只是这样做

def user_login():  # login function
    uname = input("Enter username:")
    pswd = input("Enter password:")

    with open('logindata.txt', 'r') as f: # here I open logindata.txt in read mode and I call it f
        credentials = f.read().split('\n') # I make a list with the credentials by splitting the lines of the file (I think your file has 2 lines: username and password)

    if uname == credentials[0] and pswd == credentials[1]: # check if input credentials are the same as stored credentials
        return "Accepted"
    else:
        return "Wrong username/password"

If none of these codes works I suggest to include the function that stores the credentials in logindata.txt .如果这些代码都不起作用,我建议包括在logindata.txt中存储凭据的函数 If you need help to be able to have multiple accounts to log in you should definitely add that function.如果您需要帮助才能有多个帐户登录,您一定要添加该功能。 Hope this solved your problem希望这解决了你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM