微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 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 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?