【发布时间】:2021-10-03 16:07:16
【问题描述】:
我正在运行一个脚本,该脚本在内部调用一个 bash 命令并相应地解析输出。 例如我正在调用这个命令
result = subprocess.check_output("rg result",shell=True)
print(output)
我得到这个输出没有任何行号或任何东西
historyChecker.py: result = subprocess.check_output("rg --help",shell=True)
historyChecker.py: output = re.search(r'USAGE:',result)
如果我在 bash 中运行相同的命令,我会得到不同的结果
[~/history_checker/code]$ rg result
historyChecker.py
56: result = subprocess.check_output("rg --help",shell=True)
57: output = re.search(r'USAGE:',result)
知道为什么会发生这种情况以及我们如何解决这个问题。 谢谢
【问题讨论】:
-
所以基本上你在问为什么一个“终端”的格式与另一个不同?
-
有些程序会根据标准输出是否为终端来改变其输出格式。
标签: python bash format subprocess ripgrep