我正在使用子进程在我的Python代码中使用终端命令,我试图检查communication()函数以检查该函数返回什么,并查看其中是否包含某些内容。我的函数当前取决于板的结果返回以下两项:
(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")
从该代码可以看出,它应该显示“无结果!”但是它会打印“找不到印版”,如果函数返回的印版是SBG984,则代码仍将返回“无结果!”。我猜我缺少一些简单的东西,也许有人可以发现它。
答案 0 :(得分:2)
alpr_out
是一个元组:(b'No license plates found.\n', None)
您要做的是检查子字符串是in
元组的第一个元素,而不是元组本身:
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")