【问题标题】:How can I display max column width and write into a file with max width in Python?如何在 Python 中显示最大列宽并写入具有最大宽度的文件?
【发布时间】:2021-05-28 01:50:01
【问题描述】:

我试图在 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]

【问题讨论】:

    标签: python column-width truncated


    【解决方案1】:

    任何文本都可以被截断

    MAX = 80
    print(text[:MAX])
    

    所以这样很容易。

    从您的 cmets 编辑。我看不到您发布的任何图片,但这是我的建议。

    如果你有一个长字符串(让我们回答这个问题的一部分)作为一些长字符串。

    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
    

    【讨论】:

    • 感谢您的回复。实际上,我正在尝试获取一个包含文本的未截断列,并将其保存为输出文件(例如 csv)。问题是, ('display.max_colwidth', None) 在相关列中没有给我未截断的文本。
    • 当您说“保存”时,我认为您的意思是文件,并且不受显示影响。如果你这样做 >>> d = ['kjasdhfkjashflkjasdhfklhjasdklfjhasdklfjhasdjhfajhfklsjhfjhlajhfkljshkljshfdsdkjhfasl'] >>> with pd.option_context('display.max_colwidth', None): print(pd.DataFrame(d)) 对你有用。如果是这样,您可以发布示例以显示您的错误吗?
    • 谢谢。我在上面添加了一张照片(请使用链接打开它,因为我还不允许在这里发布照片)。它显示了文本最终如何用省略号截断......我想将数据框写入文件而不截断文本。希望这很清楚。
    • 我看不到任何图片,所以我只是编辑我的答案以进一步建议该怎么做。祝你好运
    • 感谢 Peter 抽出宝贵时间,fp 是个好主意。解决了我的问题。文本不是以省略号结尾的,我可以这样写。再次感谢和亲切的问候。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 2015-04-06
    相关资源
    最近更新 更多