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

标签 python newline word-wrap

在使用 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/

相关文章:

python - 将Perl Regex语句转换为Python Regex

ios - 没有自动换行的多行 UILabel?

html - 在 GitHub 差异查看器中包装一长串 HTML 文档?

python - python 中的线程池没有预期的那么快

python - 如何拆分字符串中的大写字母?

jquery - 文本区域中的新行将转换为 <br/>

python - 如何从字符串列表中删除\n

javascript - 将 textareas 字符串值转换为以换行符分隔的 JavaScript 数组

c++ - 用 boost.python 包装结构列表

python - 与 Sympy 中的非交换符号集成