python - Python IF 语句与 Communicate() 函数
问题描述
我在使用子进程的 Python 代码中使用终端命令,我正在尝试检查通信()函数以检查函数返回的内容并查看其中是否包含某些内容。我的函数 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")
从这段代码可以看出,它应该打印“没有结果!” 但它改为打印“Plate Not Found”,如果函数返回 SBG984 的板,代码仍将返回“No results!”。我猜我错过了一些简单的东西,也许有人能发现它。
解决方案
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")
推荐阅读
- javascript - 相反的反应组件状态改变
- android - 导入 volley 库后,我的 gradle 构建失败
- mysql - 如何在 MySQL 中使用准备好的语句创建动态列?
- spring - Spring启动多个数据源以在没有@Qualifier的情况下调用相同的服务
- javascript - 如何在节点js中阻止来自其他域的请求
- visual-studio-code - 如何在 Visual Studio 代码中关闭保存时的文本格式?
- python - Django 测试:AttributeError:'Client' 对象没有属性'get'
- r - 如何读取字段中包含逗号的列的 csv 文件?
- ssh - SSH 不适用于思科集成多业务路由器
- flutter - Chewie颤动视频播放器