【问题标题】:Unable to format output data from Linux using Python3 [duplicate]无法使用 Python3 格式化来自 Linux 的输出数据 [重复]
【发布时间】:2017-01-11 01:41:51
【问题描述】:

我尝试了许多策略来重新组织从我的查询中收到的输出,但我无法格式化收到的输出。我目前正在尝试使用“diff”命令比较两个文件,但它会将所有差异发送回去。我试图通过将其设置为“diff -w”来重新格式化它,但 Popen 似乎无法同时识别这两个命令。基本上我正在尝试比较两个 Linux 文件并能够格式化返回的输出,以便用户可以轻松查看正在进行的更改。

#!/usr/bin/env python3
import subprocess

if(input("If you're happy with the changes say yes:") == "yes"):

    test = subprocess.Popen(['diff','-w','/file1','/file2'],stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
    output =  test.communicate()[0]
    print(output)

【问题讨论】:

  • 定义“混合在一起”?使用subprocess 运行命令并没有什么特别之处;您得到的结果与diff 输出的结果完全相同。

标签: python linux python-3.x subprocess


【解决方案1】:

您需要在Popen 构造函数中设置universal_newlines=True

【讨论】:

    猜你喜欢
    • 2020-11-14
    • 1970-01-01
    • 2014-09-30
    • 2017-06-02
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    相关资源
    最近更新 更多