如何在Python的子过程输出中搜索特定的单词? [英] How do you search subprocess output in Python for a specific word?
本文介绍了如何在Python的子过程输出中搜索特定的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试搜索变量的输出以找到特定的单词,然后让该单词触发响应(如果为True).
I'm trying to search through a variable's output to find a specific word, then have that trigger a response if True.
variable = subprocess.call(["some", "command", "here"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for word in variable:
if word == "myword":
print "something something"
我确定我在这里错过了一些大事,但我只是不知道这是什么.
I'm sure I'm missing something big here, but I just can't figure out what it is.
预先感谢您让我直率.
Thanks in advance for setting me straight.
推荐答案
您需要检查流程的标准输出,可以执行以下操作:
You need to check the stdout of the process, you can do something like that:
mainProcess = subprocess.Popen(['python', file, param], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
communicateRes = mainProcess.communicate()
stdOutValue, stdErrValue = communicateRes
# you can split by any value, here is by space
my_output_list = stdOutValue.split(" ")
# after the split we have a list of string in my_output_list
for word in my_output_list :
if word == "myword":
print "something something"
这是用于stdout的,您也可以检查stderr,这也是有关 split
This is for stdout, you can check the stderr also, Also here is some info about split
这篇关于如何在Python的子过程输出中搜索特定的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文