带有Communicate()函数的Python IF语句

带有Communicate()函数的Python IF语句,python,raspberry-pi,subprocess,Python,Raspberry Pi,Subprocess,我在使用子进程的Python代码中使用terminal命令,我试图检查communicate()函数以检查函数返回的内容,并查看其中是否包含内容。My function current(我的功能电流)根据铭牌的结果返回以下两项: (b'No license plates found.\n', None) Plate Not Found (b'plate0: 10 results\n - SBG984\t confidence: 85.7017\n - SBG98\t confid

我在使用子进程的Python代码中使用terminal命令,我试图检查communicate()函数以检查函数返回的内容,并查看其中是否包含内容。My function current(我的功能电流)根据铭牌的结果返回以下两项:

(b'No license plates found.\n', None) 
Plate Not Found

(b'plate0: 10 results\n    - SBG984\t confidence: 85.7017\n    -
SBG98\t confidence: 83.3453\n    - S8G984\t confidence: 78.3329\n    -
5BG984\t confidence: 76.6761\n    - S8G98\t confidence: 75.9766\n    -
SDG984\t confidence: 75.5532\n    - 5BG98\t confidence: 74.3198\n    -
SG984\t confidence: 73.3743\n    - SDG98\t confidence: 73.1969\n    -
BG984\t confidence: 71.7671\n', None) Plate Not Found
代码如下:

def read_plate():
    alpr_out = alpr_subprocess().communicate()
    print(alpr_out)
    if "No license plates found." in alpr_out:
        print ("No results!")
    elif "SBG984" in alpr_out:
        print ("Found Plate")
    else:
        print("Plate Not Found")

从这段代码可以看出,它应该打印“No results!”,但是它打印的是“Plate Not Found”,如果函数返回的是板SBG984,代码仍然会返回“No results!”。我猜我错过了一些简单的东西,也许有人能发现

alpr\u out
是一个元组:
(b'未找到车牌。\n',无)

您要做的是检查子字符串是否位于元组的第一个元素中,而不是元组本身中:

def read_plate():
    alpr_out = alpr_subprocess().communicate()
    print(alpr_out)
    # Index first element with [0]
    if "No license plates found." in alpr_out[0].decode():
        print ("No results!")
    elif "SBG984" in alpr_out[0].decode():
        print ("Found Plate")
    else:
        print("Plate Not Found")

alpr\u out
是一个元组:
(b'未找到车牌。\n',无)

您要做的是检查子字符串是否位于元组的第一个元素中,而不是元组本身中:

def read_plate():
    alpr_out = alpr_subprocess().communicate()
    print(alpr_out)
    # Index first element with [0]
    if "No license plates found." in alpr_out[0].decode():
        print ("No results!")
    elif "SBG984" in alpr_out[0].decode():
        print ("Found Plate")
    else:
        print("Plate Not Found")

print(alpr_out)
print(type(alpr_out))
的输出是什么?打印在上面的问题中,但是类型是@arsho。请仔细检查代码中的变量类型。@BaileyKocin什么是变量类型?alpr_out是tuple吗?应该仍然能够检查一些东西是否像我所做的那样被包含it@CurtusB我们只是想确定它是一个元组。我们如何确定?打印(alpr_out)和打印(type(alpr_out))的输出是什么?打印在问题的上面,但是类型是@arsho。请仔细检查代码中的变量类型。@BaileyKocin什么是什么意思?什么变量类型?alpr_out是tuple吗?应该仍然能够检查一些东西是否像我所做的那样被包含it@CurtusB我们只是想确定它是一个元组。我们如何确定?您的代码返回错误,我会将其添加到您的answer@CurtisB现在检查,我添加了
decode()
将字节转换为字符串如果您的代码返回错误,我会将其添加到您的answer@CurtisB现在检查,我添加了
decode()
以将字节转换为字符串