gpt4 book ai didi

Python textwrap 库 - 如何保留换行符?

转载 作者:太空狗 更新时间:2023-10-29 17:24:22 25 4
gpt4 key购买 nike

在使用 Python 的 textwrap 库时,如何将其转换为:

short line,

long line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

进入这个:

short line,

long line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx

我试过:

w = textwrap.TextWrapper(width=90,break_long_words=False)
body = '\n'.join(w.wrap(body))

但是我得到:

short line, long line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

(在我的示例中间距不精确)

最佳答案

尝试

w = textwrap.TextWrapper(width=90,break_long_words=False,replace_whitespace=False)

这似乎解决了我的问题

我从阅读的内容中得出结论 here (我以前从未使用过文字环绕)

关于Python textwrap 库 - 如何保留换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1166317/

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