我试图在Python中完整地显示一列(包含tweet文本),但即使我使用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 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.
1 Tanks 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
>>>
然而,我不会使用这种方法来显示任何内容。你要么自己格式化,运行行,然后自己格式化每个单元格。或者与推文中可能包含的未经过滤的控制字符相抗衡。
你可以用我建议的第一种方法很容易地遍历:
>>> _ = [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
如果你不想遍历数据,你可以使用tabulate来格式化数据帧。
>>> from tabulate import tabulate
>>> print(tabulate(df))
- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 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.
1 Tanks 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
- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
或者使用两种方法的组合。但我不是熊猫输出的粉丝,因为它比它的价值更麻烦。
print将打印到一个文件,如果你提供了一个fp。
>>> 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, but even if I use pd.set_option('display.max_colwidth', None), the output
Tanks 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