python - 如何在 Python 中显示最大列宽并写入具有最大宽度的文件?
问题描述
我正在尝试在 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 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
如果您不想遍历数据,可以使用制表来格式化数据框。
>>> 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
- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
或者结合使用这两种方法。但我不喜欢 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, 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
推荐阅读
- python - 解析没有前导零、没有空格和没有反冲的日期
- javascript - 使用样式化组件 React.js 定位特定类
- authentication - Laravel 7 根据用户值重定向登录
- vb.net - 多线程定时调整
- java - SpringBoot中使用@Valid时如何自定义验证失败的响应
- c - 为什么 gcc 使“!(T_MAX+T_MAX+2)”等于 0?
- jquery - 页面加载非常慢
- android - 在 Android Studio 上找不到 CMake 的 LZ4
- c# - unity OnInspectorGUI 函数调用 PrefabUtility.ReplacePrefab 函数 Nullreference?
- docker - Docker、webpack 和环境变量