Python 使用 perl 进行格式化的子进程给出了不完整的输出
Python subprocess using perl for formatting is giving incomplete output
我在读取 python 子进程命令的输出时遇到问题。
我想从中读取输出的 bash 命令:
pacmd list-sink-inputs | tr '\n' '\r' | perl -pe 's/ *index: ([0-9]+).+?application\.process\.id = "([^\r]+)"\r.+?(?=index:|$)/:\r/g' | tr '\r' '\n'
当我通过 bash 运行 时,我得到了预期的输出:
4 sink input(s) available.
6249:72
20341:84
20344:86
20350:87
当我尝试通过 python 的子进程 运行 获取它的输出时:
subprocess.Popen(cmnd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('UTF-8')
check_output(cmnd,shell=True).decode('UTF-8')
subprocess.run(cmnd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode('utf-8')
其中 cmnd = """pacmd list-sink-inputs | tr '\n' '\r' | perl -pe 's/ *index: ([0-9]+).+?application\.process\.id = "([^\r]+)"\r.+?(?=index:|$)/:\r/g' | tr '\r' '\n'"""
它给出以下输出:
'4 sink input(s) available.\n\x02:\x01\n\x02:\x01\n\x02:\x01\n\x02:\x01\n'
这是意外的,因为它没有 6249:72 等。我想要的数字。甚至 stderr 都是空白的,并且 returncode 是预期的 0。
我能找到的唯一解决方法是将 bash 输出重定向到文本文件,然后通过 python 读取文本文件,我不想使用它,因为那是不必要的文件IO.
我已经经历了 Missing output from subprocess command, Python Subprocess Grep, Python subprocess run() is giving abnormal output [duplicate] 和许多其他的经历,但我无法理解到底出了什么问题。
您有一个报价问题。 """"""
表示 chr(0o1)
。要生成字符串 </code>,您可以使用 <code>"""\1"""
。 \
的其他实例也应该是 \
。
由于 \
的所有实例都需要转义,您还可以使用 r""""""
.
其他问题:
</code> 和 <code>
在正则表达式之外无论如何都是错误的。您应该使用 </code> 和 <code>
.
这里没有使用多行文字。 "..."
或 r"..."
就足够了。
整个tr
业务可以通过使用-0777
避免导致perl
将整个文件视为一行。
这给了我们:
cmnd = "pacmd list-sink-inputs | perl -0777pe's/ *index: (\d+).+?application\.process\.id = "([^\n]+)"\n.+?(?=index:|$)/:\n/sag'"
或
cmnd = r"pacmd list-sink-inputs | perl -0777pe's/ *index: (\d+).+?application\.process\.id = "([^\n]+)"\n.+?(?=index:|$)/:\n/sag'"
但是为什么在这里使用 Perl?您可以在 Python!
中轻松地做同样的事情
我在读取 python 子进程命令的输出时遇到问题。
我想从中读取输出的 bash 命令:
pacmd list-sink-inputs | tr '\n' '\r' | perl -pe 's/ *index: ([0-9]+).+?application\.process\.id = "([^\r]+)"\r.+?(?=index:|$)/:\r/g' | tr '\r' '\n'
当我通过 bash 运行 时,我得到了预期的输出:
4 sink input(s) available.
6249:72
20341:84
20344:86
20350:87
当我尝试通过 python 的子进程 运行 获取它的输出时:
subprocess.Popen(cmnd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('UTF-8')
check_output(cmnd,shell=True).decode('UTF-8')
subprocess.run(cmnd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode('utf-8')
其中 cmnd = """pacmd list-sink-inputs | tr '\n' '\r' | perl -pe 's/ *index: ([0-9]+).+?application\.process\.id = "([^\r]+)"\r.+?(?=index:|$)/:\r/g' | tr '\r' '\n'"""
它给出以下输出:
'4 sink input(s) available.\n\x02:\x01\n\x02:\x01\n\x02:\x01\n\x02:\x01\n'
这是意外的,因为它没有 6249:72 等。我想要的数字。甚至 stderr 都是空白的,并且 returncode 是预期的 0。
我能找到的唯一解决方法是将 bash 输出重定向到文本文件,然后通过 python 读取文本文件,我不想使用它,因为那是不必要的文件IO.
我已经经历了 Missing output from subprocess command, Python Subprocess Grep, Python subprocess run() is giving abnormal output [duplicate] 和许多其他的经历,但我无法理解到底出了什么问题。
您有一个报价问题。 """"""
表示 chr(0o1)
。要生成字符串 </code>,您可以使用 <code>"""\1"""
。 \
的其他实例也应该是 \
。
由于 \
的所有实例都需要转义,您还可以使用 r""""""
.
其他问题:
</code> 和 <code>
在正则表达式之外无论如何都是错误的。您应该使用</code> 和 <code>
.这里没有使用多行文字。
"..."
或r"..."
就足够了。整个
tr
业务可以通过使用-0777
避免导致perl
将整个文件视为一行。
这给了我们:
cmnd = "pacmd list-sink-inputs | perl -0777pe's/ *index: (\d+).+?application\.process\.id = "([^\n]+)"\n.+?(?=index:|$)/:\n/sag'"
或
cmnd = r"pacmd list-sink-inputs | perl -0777pe's/ *index: (\d+).+?application\.process\.id = "([^\n]+)"\n.+?(?=index:|$)/:\n/sag'"
但是为什么在这里使用 Perl?您可以在 Python!
中轻松地做同样的事情