简体   繁体   English

评估列表python中的变量

[英]Evaluating if variable in list python

I am using a RFID reader to scan multiple RFID tags. 我正在使用RFID阅读器扫描多个RFID标签。 The readings are placed into a list. 读数放入列表中。 I am trying to check if a tag is in the reading. 我正在尝试检查标签是否在阅读中。 Using the 'if in' method is not working for me. 使用'if in'方法对我不起作用。

import mercury
import time

reader = mercury.Reader("tmr:///dev/ttyUSB0")
reader.set_region("EU3")
reader.set_read_plan([1], "GEN2")

tag1 = 'E2004005730702602190360B'
tag2 = 'E20040057307026421903619'

while True:
    a = reader.read()
    print (a)
    print(type(a))
    if tag1 in a:
            print('tag1')
            time.sleep(0.2)
            break
    if tag2 in a:
            print('tag2')
            time.sleep(0.2)
            break
    time.sleep(0.2)

My terminal is output is: 我的终端输出是:

['E20040057307026421903619', 'E2004005730702602190360B']
<type 'list'>

So the if conditions is not executed when tag1 or tag2 are in a. 因此当tag1或tag2在a中时,不执行if条件。

I can't seem to make it enter the if condition. 我似乎无法让它进入if条件。 Any advice? 有什么建议?

It turns out the below answer doesn't work here, since "reader" is an object other than a file and a is a list already. 事实证明,下面的答案在这里不起作用,因为“reader”是一个文件以外的对象,而a已经是一个列表。 A better solution for you might be to change your "if in"s to if any(tag1 == tag.epc for tag in a) . 更好的解决方案可能是将“if in”更改为if any(tag1 == tag.epc for tag in a) Printing is misleading in this case since it seems the list is actually filled with objects that print as strings but won't compare equal to strings. 在这种情况下打印会产生误导,因为看起来列表实际上填充的是打印为字符串但不会与字符串相等的对象。

I'm going to leave the earlier answer since it might be helpful to people with similar problems. 我将留下较早的答案,因为它可能对有类似问题的人有所帮助。

======================================================================= ================================================== =====================

Assuming "reader" is a file, using reader.read() doesn't return a list. 假设“reader”是一个文件,使用reader.read()不返回列表。 It returns a string. 它返回一个字符串。 You won't get an interpreter error because strings are iterable, but you also won't get the results you expect! 你不会得到一个解释器错误,因为字符串是可迭代的,但你也不会得到你期望的结果!

EDIT: Using string1 in string2 will return True iff string1 is a substring of string2. 编辑: string1 in string2使用string1 in string2将返回True iff string1是string2的子字符串。 However, it is still important to be careful, because other operations that are valid (like a[0] ) will return unexpected results. 但是,要小心仍然很重要,因为其他有效的操作(如a[0] )将返回意外结果。

Try 尝试

import ast
a = ast.literal_eval(reader.read())

That will try to interpret the result of .read() as a list. 这将尝试将.read()的结果解释为列表。 If that doesn't work for you, look into regular expressions . 如果这对您不起作用,请查看正则表达式

Looking at the documentation , it appears that you do not get a list of str , but TagReadData objects. 查看文档 ,看来您没有获得str的列表,而是TagReadData对象。

Try along these lines: 尝试以下这些方面:

tag1 = b'E2004005730702602190360B'
tag2 = b'E20040057307026421903619'

a = reader.read()
for tag in a:
    if tag1 == tag.epc:
        print('tag1')

你应该适当地打印列表对象的上下文a(reader.read())它将告诉你内容或目的对象,然后理解什么是比较(if语句)

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

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