如何测试字典(在外部文件中)是否包含用户输入的用户名(Python3)?

2024-05-16 01:27:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在建立一个跳蚤市场计划。我有一个外部文件托管所有员工的用户名和密码。我试图测试登录部分,要求用户名,然后密码。它测试UN是否在readline()中包含的字典中。你知道吗

这是带有用户名和密码的外部文件。地址:

managers = {"manager":"password", "owner":"apple"}
employees = {"jane":"none", "john":"banana"}

这是密码:

print("Welcome to Flea Master 2000...\n")
read_employee_file = open('employees_list.txt', 'r')
managers = read_employee_file.readline(0)
employees = read_employee_file.readline(1)
print(managers)
read_employee_file.close()

user_id = input("User ID:\n")
user_password = input('Password:\n')
if user_id in managers[:]:
    if managers[user_id] == user_password:
        print("Welcome, {0}.".format (user_id))
        user_status='manager'
if user_id in employees:
    if employees[user_id] == user_password:
        print("Welcome, {0}".format (user_id))
        user_status = 'staff'
if user_status == 'manager':
    action_manager = int(input("Options: (Input number to select...)\n1) Add employee.\n2) Remove employee.\n"))
    if action_manager == 1:
        employee_addition_type=input("What kind of employee is he/she? ('manager' or 'staff')")
        if employee_addition_type == 'manager':
            new_manager_username = input("Enter the new manager's username...\n")
            new_manager_password = input("Enter the new manager's password...\n")
            managers[new_manager_username] = new_manager_password
        else:
            new_staff_username = input("Enter the new staff member's username...\n")
            new_staff_password = input("Enter the new staff member's password...\n")
            employees[new_staff_username]=new_staff_password

    if action_manager == 2:
        print("The list of current employees is: \n")
        for key in all_staff:
            print(key)
        print('\n')
        which_remove = input("Now, which do you want to remove? Enter the username exactly.\n")
        if which_remove in managers:
            del managers[which_remove]
        else:
            del employees[which_remove]
        print("\nDone. Updated roster is:\n")
        all_staff = dict(managers, **employees)
        for key in all_staff:
            print(key
                  )

Tags: inidnewinputifusernameemployeemanager
2条回答

你的readline行有点不正确。readlines的参数是它将读取的最大字节数。所以readlines(6)并不意味着“读第六行”,而是意味着“从当前行读不超过六个字符”。我建议只做read_employee_file.readline(),不要争论。你知道吗

managers = read_employee_file.readline()
employees = read_employee_file.readline()

现在您已经拥有了每一行的全部内容,但这两个变量仍然是字符串。但是,您可以使用json模块加载这些字典。你知道吗

import json
line = 'managers = {"manager":"password", "owner":"apple"}'

#chop off the left part of the string containing "managers = "
line = line.partition(" = ")[2]
d = json.loads(line)

print "the owner's password is", d["owner"]

if "bob" not in d:
    print "can't find bob's password in managers dictionary!"

结果:

the owner's password is apple
can't find bob's password in managers dictionary!

有几种方法可以读取和解析输入文件。假设您的文件是您指定的方式,并且您愿意处理异常,下面是一个示例方法。您需要适当地处理异常。你知道吗

try:
    #This will read your in.conf which contains user/pwds in the dictionary format you specified.
    #Read documentation on exec here: 
       # https://docs.python.org/3.0/library/functions.html#exec
    with open('in.conf') as fh:
        for line in fh:
            exec(line)
    user_id = input("User ID:\n")
    user_password = input('Password:\n')
    if user_id in managers and user_password == managers[user_id]:
        print("Welcome, {0}.".format (user_id))
        user_status='manager'
    elif user_id in employees and user_password == employees[user_id]:
        print("Welcome, {0}.".format (user_id))
        user_status='staff'
    else:
        print "invalid credentials"
except NameError:
    #catch situations where your file doesn't contain managers or employees dictionary
    #I just raise it so you can see what it would print
    raise
except:
    #other exceptions as you see appropriate to handle ....
    #I just raise it so you can see what it would print
    raise

相关问题 更多 >