如何在Python字典中匹配键和值?
在名为“如何在Python字典中匹配键和值?,python,io,Python,Io,在名为“user\u table.txt”的文本文件中,我有以下代表用户名和密码的数据: 然后,我使用以下代码创建Python字典: users = {} with open("user_table.txt", 'r') as file: for line in file: line = line.strip() # if there is no password if ('-' in line) == Fa
user\u table.txt
”的文本文件中,我有以下代表用户名和密码的数据:
然后,我使用以下代码创建Python字典:
users = {}
with open("user_table.txt", 'r') as file:
for line in file:
line = line.strip()
# if there is no password
if ('-' in line) == False:
continue
# otherwise read into a dictionary
else:
key, value = line.split(' - ')
users[key] = value
k= users.keys()
print(k)
以下代码是使用一些简单规则进行身份验证的函数:
a) 如果用户已登录(基于登录状态),则提示
b) 如果文件中不存在用户名,则提示
c) 如果用户名存在,但与文件中的密码不匹配,则提示
以下是(尝试)实现此功能的函数:
def authenticate(login_status, username, password):
with open("user_table.txt", 'r') as file:
for line in file:
line = line.strip()
# if there is no password
if ('-' in line) == False:
continue
# otherwise read into a dictionary
else:
key, value = key, value = line.split(' - ')
users[key] = value.strip()
user_table= users.items()
user_names = user_table.keys()
# print(k)
if login_status == True:
print('User {} is already logged in.'.format(username))
if username not in user_table.keys():
print('username is not found')
if username in user_table.keys() and password != user_table.get('value'):
print('the password is incorrect')
if username in user_table.keys() and password == user_table.get('value'):
print('welcome to our new application')
当我调用以下命令时:
authenticate(False, 'Brian', 'briguy987321CT')
我得到这个错误:
AttributeError: 'dict_items' object has no attribute 'keys'
错误是由以下原因引起的:
if username not in user_table.keys():
知道错误在哪里吗
提前谢谢 根据@khelwood的建议,以下编辑解决了错误消息:
# user_table= users.items()
# user_names = user_table.keys()
# print(k)
if login_status == True:
print('User {} is already logged in.'.format(username))
if username not in users.keys():
print('username is not found')
if username in users.keys() and password != users.get('value'):
print('the password is incorrect')
if username in users.keys() and password == users.get('value'):
print('welcome to our new application')
user\u table
不是一个字典,而是一个类似集合的对象,它将字典中的每个条目作为元组保存(即,{0:'a',1:'b',2:'c'}.items()
变成dict\u items([(0,'a'),(1,'b'),(2,'c')])
。此对象没有.keys()
方法
我认为你应该做的只是使用字典用户
:
如果登录状态==True:
打印('用户{}已登录。'.format(用户名))
如果用户名不在users.keys()中:
打印('找不到用户名')
如果(username in users.keys())和(password!=users.get('value')):
打印('密码不正确')
如果(username in users.keys())和(password==users.get('value')):
打印(“欢迎使用我们的新应用程序”)
(括号只是为了可读性)为什么要使用
.items()
?用户
是一个dict。它有键
和获取
,您可以很容易地在其中查找内容。这是刚刚从这个S/O站点获得的代码吗?这回答了您的问题吗?是的,谢谢。user_table=users.items()user\u names=user.keys()这里users是一个字典,users.items()将返回列表,要获取密钥列表,请使用“user.keys()”而不是“user\u table.keys()”如果login\u status==True
则应为如果login\u status
并且如果username不在users.keys()中
如果username不在users中,则应为。对。我没有充分考虑我的编辑,我认为您仍然可以更改您的答案。
# user_table= users.items()
# user_names = user_table.keys()
# print(k)
if login_status == True:
print('User {} is already logged in.'.format(username))
if username not in users.keys():
print('username is not found')
if username in users.keys() and password != users.get('value'):
print('the password is incorrect')
if username in users.keys() and password == users.get('value'):
print('welcome to our new application')