我知道如何搜索子进程输出中是否存在特定单词。但是,如果我知道这一行总是如下所示,我怎么才能打印出一条特定的行:
This is the line: some text
"一些文字"可以有不同的价值观。
我有什么:
variable = subprocess.call(["some", "command"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = output.communicate()
执行脚本后我想要的是:
This is the line: some text
答案 0 :(得分:1)
import subprocess
p = subprocess.Popen(["some", "command"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
stdout=stdout.split("\n")
for line in stdout:
if line.startswith("This is the line:"):
print line