gpt4 book ai didi

python - 如何在 `textwrap.fill` 中保留双换行符?

转载 作者:太空宇宙 更新时间:2023-11-04 01:16:03 27 4
gpt4 key购买 nike

我有一个文本,由几个段落组成,段落之间用双换行符分隔。我想将它们格式化为 70 行宽,保留新行,整个内容应该以一个非缩进行开始,并带有 Abstract: Lorem ipsum ...

所以整个事情应该是这样的:

Abstract: Magna risus nonummy mollis mattis neque commodo mattis fusce  
hendrerit nibh. Lorem massa lorem mauris ad orci quam risus
viverra aliquet senectus sociis. Donec proin nam dolor neque
placerat imperdiet eros ullamcorper egestas cum torquent
habitasse. Risus donec odio nostra ac et pede inceptos
praesent montes. Neque morbi sit morbi vestibulum
suspendisse mauris. Lacus massa mollis.

Donec class integer pede ac sed elit. Fames augue magnis
sapien natoque nisi. Proin augue mus nisl interdum convallis
pellentesque conubia.

Class dolor tempor netus suspendisse odio orci
vestibulum mus. Netus purus. Lacus metus tempor purus
adipiscing faucibus eget maecenas. Velit lacus integer
rhoncus primis nunc quis lorem lacus dictumst hendrerit.

我正在尝试使用 textwrap,但这不会产生所需的输出。这是代码:

from loremipsum import get_paragraphs
import textwrap

text = '\n\n'.join(get_paragraphs(3))
item = 'Abstract: '

print textwrap.fill(item+text,initial_indent='',subsequent_indent=' '*len(item),replace_whitespace=False)

这对于第一段来说效果很好,但是接下来的段落会出现一些奇怪的缩进和短行,就像这样

Class vitae
nonummy imperdiet cras blandit fusce. Massa porta metus
semper tempor non id viverra eget. Purus morbi lorem semper
eget. Proin magna tortor metus magnis. Vitae ipsum. Velit
class aliquet tortor dolor parturient ullamcorper libero ac.

即使我使用 initial_indent=' '*len(item) 也会发生这种情况。这是一个错误吗?我怎样才能得到我想要的?

最佳答案

来自文档:

Note: If replace_whitespace is false, newlines may appear in the middle of a line and cause strange output. For this reason, text should be split into paragraphs (using str.splitlines() or similar) which are wrapped separately.

所以你应该这样做:

paragraphs = get_paragraphs(3)
item = 'Abstract: '
paragraphs[0] = item + paragraphs[0]
for idx, paragraph in enumerate(paragraphs):
rest_indent = " "*len(item)
start_indent = "" if idx == 0 else rest_indent
print textwrap.fill(paragraph,initial_indent=start_indent,subsequent_indent=rest_indent,replace_whitespace=False)
print ""

或者,使用列表理解:

paragraphs = get_paragraphs(3)
item = 'Abstract: '
text = "\n\n".join(textwrap.fill(p,initial_indent=' '*len(item),subsequent_indent=' '*len(item)) for p in paragraphs)
print item + text.lstrip()

关于python - 如何在 `textwrap.fill` 中保留双换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24615988/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com