gpt4 book ai didi

python - 你如何在 Python 中搜索子进程输出的特定词?

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:49 25 4
gpt4 key购买 nike

我正在尝试搜索变量的输出以找到特定的词,然后在为真时触发响应。

variable = subprocess.call(["some", "command", "here"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

for word in variable:
if word == "myword":
print "something something"

我确定我在这里遗漏了一些重要的东西,但我就是想不通它是什么。

预先感谢您让我直截了当。

最佳答案

你需要检查进程的标准输出,你可以这样做:

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 的信息

关于python - 你如何在 Python 中搜索子进程输出的特定词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23272184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com