Python 在 PyCharm 的内置控制台中获得更广泛的输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25628496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:24:36  来源:igfitidea点击:

Getting wider output in PyCharm's built-in console

pythonpandasipythonpycharm

提问by mattvivier

I'm relatively new to using the PyCharm IDE, and have been unable to find a way to better shape the output when in a built-in console session. I'm typically working with pretty wide dataframes, that would fit easily across my monitor, but the display is cutting and wrapping them much sooner than needed.

我对使用 PyCharm IDE 比较陌生,并且无法找到一种在内置控制台会话中更好地塑造输出的方法。我通常使用非常宽的数据帧,这很容易适合我的显示器,但显示器切割和包装它们的速度比需要的要快得多。

Does anyone know of a setting to change this behavior to take advantage of the full width of my screen?

有谁知道更改此行为以利用我的屏幕的整个宽度的设置?

Edit: I don't have enough reputation to post a screenshot, but link is below: http://imgur.com/iiBK3iU

编辑:我没有足够的声誉来发布截图,但链接如下:http: //imgur.com/iiBK3iU

I would like to prevent it from wrapping after only a few columns (for example, the column 'ReadmitRate' should be immediately to the right of 'SNFDaysPerSNFCase')

我想防止它仅在几列之后换行(例如,“ReadmitRate”列应立即位于“SNFDaysPerSNFCase”的右侧)

采纳答案by mattvivier

It appears I was mistaken in thinking that the issue was one in PyCharm (that could be solved, for example, in a setting or preference.) It actually has to do with the console session itself. The console attempts to auto-detect the width of the display area, but when that fails it defaults to 80 characters. This behavior can be overridden with:

我似乎错误地认为问题出在 PyCharm 中(例如,可以在设置或首选项中解决)。它实际上与控制台会话本身有关。控制台尝试自动检测显示区域的宽度,但如果失败,则默认为 80 个字符。此行为可以被覆盖:

import pandas as pd
desired_width = 320    
pd.set_option('display.width', desired_width)

Where you can of course set the desired_widthto whatever your display will tolerate. Thanks to @TidB for the suggestion that my initial concern wasn't focused in the right area.

您当然可以将 设置desired_width为您的显示器可以容忍的任何内容。感谢@TidB 的建议,即我最初的担忧没有集中在正确的领域。

回答by Contango

The answer by @mattvivierworks nicely when printing Pandas dataframes (thanks!).

@mattvivier打印 Pandas 数据帧时,答案很有效(谢谢!)。

However, if you are printing NumPy arrays, you need to set np.set_printoptionsas well:

但是,如果您要打印 NumPy 数组,则还需要设置np.set_printoptions

import pandas as pd
import numpy as np
desired_width = 320
pd.set_option('display.width', desired_width)
np.set_printoptions(linewidth=desired_width)

See docs on NumPy and set_printoptions.

请参阅NumPy 和 set_printoptions 上的文档

回答by Idodo

For me, just setting 'display.width'wasn't enough in pycharm, it kept displaying in truncated form.

对我来说,'display.width'在 pycharm 中仅仅设置是不够的,它一直以截断形式显示。

However, adding the option pd.set_option("display.max_columns", 10)together with display width worked and I was able to see the whole dataframe printed in the "run" output.

但是,将选项pd.set_option("display.max_columns", 10)与显示宽度一起添加有效,我能够看到在“运行”输出中打印的整个数据帧。

In summary:

总之:

import pandas as pd    
pd.set_option('display.width', 400)
pd.set_option('display.max_columns', 10)