Python textwrap库-如何保留换行符? [英] Python textwrap Library - How to Preserve Line Breaks?
本文介绍了Python textwrap库-如何保留换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用Python的textwrap库时,我该如何打开它:
When using Python's textwrap library, how can I turn this:
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
(在我的示例中间隔不完全准确)
(spacing not exact in my examples)
推荐答案
尝试
w = textwrap.TextWrapper(width=90,break_long_words=False,replace_whitespace=False)
这似乎为我解决了这个问题
that seemed to fix the problem for me
我从这里(我从未使用过textwrap)
I worked that out from what I read here (I've never used textwrap before)
这篇关于Python textwrap库-如何保留换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文