[英]Why is my Python Dictionary only returning the last value?
I have an rfid reader which when scanned returns a UID as "backData"我有一个 rfid 阅读器,它在扫描时返回一个 UID 作为“backData”
I have my users (username and UID) stored in a text file.我将我的用户(用户名和 UID)存储在一个文本文件中。
I've managed to read the text file into a python dictionary, but when i scan my card, it is only accepting the last UID in the text file.我已经设法将文本文件读入 python 字典,但是当我扫描我的卡时,它只接受文本文件中的最后一个 UID。
Text File:文本文件:
164 168 124 90 42, user1
114 156 203 196 225, user2
Python Code:蟒蛇代码:
for line in uid_file:
info = line.split(",")
key = info[0]
uname = info[1]
c = len(uname)-1
uname = uname[0:c]
uid_dict[key] = uname
USER = [int(i) for i in key.split()]
if backData == USER:
f = open("/mnt/lock_logs/lock_log.csv", "a");
print f
value = ('\n') + uname
myString = str(value)
f.write(myString)
f.close()
else:
print "Access Denied"
So if I scan the card assigned to user2, it works, but if I scan the card assigned to user1, I get Access Denied.因此,如果我扫描分配给 user2 的卡,它可以工作,但如果我扫描分配给 user1 的卡,则会收到拒绝访问。
If i print the variable USER, it returns both UIDs from the text file.如果我打印变量 USER,它会从文本文件中返回两个 UID。 Any ideas on what I need to change??
关于我需要改变什么的任何想法?
I think this is because you continue to evaluate your for loop even after the user 1 is matched.我认为这是因为即使在用户 1 匹配之后,您仍继续评估您的 for 循环。 Add a 'break' to the bottom of your if statement.
在 if 语句的底部添加一个“中断”。
if backData == USER:
f = open("/mnt/lock_logs/lock_log.csv", "a");
print f
value = ('\n') + uname
myString = str(value)
f.write(myString)
f.close()
break
I recreated your file and am able to get predicted behavior with the following code.我重新创建了您的文件,并且能够使用以下代码获得预测的行为。 Not that I hard coded backData and have tried it with both USER lists with success.
并不是说我对 backData 进行了硬编码并成功地尝试了两个 USER 列表。
filename = 'uid.txt'
with open(filename) as uid_file:
uid_dict={}
backData = [164, 168, 124, 90, 42]
no_matches = True
for line in uid_file:
info = line.split(",")
key = info[0]
uname = info[1]
print info[0], info[1]
c = len(uname)-1
uname = uname[0:c]
uid_dict[key] = uname
USER = [int(i) for i in key.split()]
if backData == USER:
print "user matches"
no_matches = False
break
if no_matches:
print "Access Denied"
Reanalyzing the code a bit, I am assuming that backData is an array, such as [114, 156, 203, 196, 225]
.稍微重新分析一下代码,我假设 backData 是一个数组,例如
[114, 156, 203, 196, 225]
。
Your code is going to loop through each line, checking the array against the stored key.您的代码将遍历每一行,根据存储的键检查数组。 It won't stop if it finds a match, and it will print Access Denied each time it doesn't match (not the same as if it had not found a match at all .)
如果找到匹配项,它不会停止,并且每次不匹配时都会打印拒绝访问(与根本没有找到匹配项不同。)
A more detailed explanation:更详细的解释:
If backData matched with user1, the following will happen in order:如果 backData 与 user1 匹配,则会依次发生以下情况:
\\nuser1
is appended to lock_log.csv \\nuser1
附加到 lock_log.csvAccess Denied
is printed.Access Denied
。 If backData matched with user2, the following will happen in order:如果 backData 与 user2 匹配,则会依次发生以下情况:
Access Denied
is printed.Access Denied
。\\nuser2
is appended to lock_log.csv \\nuser2
附加到 lock_log.csv If backData matched with neither, the following will happen in order:如果 backData 与两者都不匹配,则会按顺序发生以下情况:
Access Denied
is printed.Access Denied
。Access Denied
is printed again.Access Denied
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.