问题描述
我试图在 Python 中完整显示一列(包含推文文本),但即使我使用 pd.set_option('display.max_colwidth',None),输出也会以截断形式返回。下面的代码让我显示最大宽度,但我想要的是将文件保存为最大列宽的输出。任何帮助表示赞赏。
def display_text_max_col_width(df,width):
with pd.option_context('display.max_colwidth',width):
print(df)
display_text_max_col_width(df["col"],1000)
[tweets ending with ellipsis][1]
解决方法
可以通过执行截断任何文本
MAX = 80
print(text[:MAX])
这样就很容易了。
编辑来自您的评论。我看不到您发布的任何图片,但这是我的建议。
如果你有一个长字符串(让我们来回答这个问题)作为一些长字符串。
txt1 = """ I am trying to display a column in full (containing tweet texts) in Python,but even if I use pd.set_option('display.max_colwidth',None),the output returns as truncated. The code below lets me display the maximum width,but what I want is to save the file as output with max column width. Any help is appreciated. """
txt2 = """Thanks for your response. Indeed,I am trying to get an untruncated column,which contains text,and save it as an output file (e.g.,csv) as such. The problem is,('display.max_colwidth',None) doesn't give me untruncated text in the related column. – Kamil Yilmaz 2 days ago"""
你是说这对你来说是截断或不起作用,但这对我有用。
>>> with pd.option_context('display.max_colwidth',None): print(pd.DataFrame([txt1,txt2])) ...
0
0 I am trying to display a column in full (containing tweet texts) in Python,but what I want is to save the file as output with max column width. Any help is appreciated.
1 Tanks for your response. Indeed,None) doesn't give me untruncated text in the related column. – Kamil Yilmaz 2 days ago
>>>
但是我不会使用这种显示任何内容的方法。您必须通过运行行并自己格式化每个单元格来自己格式化它。或者与推文中可能包含的未过滤控制字符的一些古怪作斗争。
您可以使用我建议的第一种方法轻松迭代:
>>> _ = [print(line[:60]) for line in df[0]]
I am trying to display a column in full (containing tweet te
Tanks for your response. Indeed,I am trying to get an untru
如果您不想遍历数据,可以使用制表来格式化数据框。
>>> from tabulate import tabulate
>>> print(tabulate(df))
- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 I am trying to display a column in full (containing tweet texts) in Python,but what I want is to save the file as output with max column width. Any help is appreciated.
1 Tanks for your response. Indeed,None) doesn't give me untruncated text in the related column. – Kamil Yilmaz 2 days ago
- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
或者结合使用这两种方法。但我不喜欢 Pandas 的输出,因为它比它的价值更麻烦。
如果您提供 fp,print 将打印到文件。
>>> with open('/tmp/text','w') as fp:
... [print(line[:150],file=fp) for line in df[0]]
...
[None,None]
>>> [print(line) for line in open('/tmp/text').readlines() ]
I am trying to display a column in full (containing tweet texts) in Python,the output
Tanks for your response. Indeed,csv) as such. The