【问题标题】:Python terminal output widthPython终端输出宽度
【发布时间】:2017-09-16 18:25:48
【问题描述】:

我在终端(在 Mac 上)中的 Python 3.5.2 输出限制为大约 ca 的宽度。 80px,即使我增加终端窗口的大小。

这个窄的宽度在输出长数组时会导致一堆换行,这真的很麻烦。如何告诉 python 使用完整的命令行窗口宽度?

作为记录,我在任何其他程序中都没有看到这个问题,例如我的 c++ 输出看起来很好。

【问题讨论】:

    标签: python python-3.x shell unix formatting


    【解决方案1】:

    我在使用pandas 时遇到了同样的问题。因此,如果这是您要解决的问题,我会通过以下方式修复我的问题 pd.set_option('display.width', pd.util.terminal.get_terminal_size()[0])

    【讨论】:

    • 在 Python 3.7 中无法使用 Pandas 0.23.3AttributeError: module 'pandas.util' has no attribute 'terminal' 失败
    【解决方案2】:

    对于 numpy,事实证明您可以通过设置启用完整输出

    np.set_printoptions(suppress=True,linewidth=np.nan,threshold=np.nan).

    【讨论】:

    • 在 Python 3.7 和 numpy 1.16.2 中,ValueError: threshold must be numeric and non-NAN, try sys.maxsize for untruncated representation 失败。但是,numpy.set_printoptions(suppress=True,linewidth=numpy.nan) 有效!
    【解决方案3】:

    在 Python 3.7 及以上版本,您可以使用

    from shutil import get_terminal_size
    pd.set_option('display.width', get_terminal_size()[0])
    

    【讨论】:

    【解决方案4】:

    2x15 矩阵的默认输出已损坏:

    a.T
    array([[ 0.2, -1.4, -0.8,  1.3, -1.5, -1.4,  0.6, -1.5,  0.4, -0.9,  0.3,
             1.1,  0.5, -0.3,  1.1],
           [ 1.3, -1.2,  1.6, -1.4,  0.9, -1.2, -1.9,  0.9,  1.8, -1.8,  1.7,
            -1.3,  1.4, -1.7, -1.3]])
    

    使用 numpy set_printoptions() 命令修复输出

    import sys
    np.set_printoptions(suppress=True,linewidth=sys.maxsize,threshold=sys.maxsize)
    a.T
    [[ 0.2 -1.4 -0.8  1.3 -1.5 -1.4  0.6 -1.5  0.4 -0.9  0.3  1.1  0.5 -0.3  1.1]
     [ 1.3 -1.2  1.6 -1.4  0.9 -1.2 -1.9  0.9  1.8 -1.8  1.7 -1.3  1.4 -1.7 -1.3]]
    

    系统和numpy版本:

    sys.version =  3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)]
    numpy.__version__ =  1.18.5
    

    【讨论】:

      猜你喜欢
      • 2018-10-02
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多