我正在用纯文本发送电子邮件。现在我有一个带有文本的字符串,我将其插入到电子邮件中。但是,我希望此文本具有最大字符宽度。
所以输入文本例如:
This is the input text. It's very boring to read because it's only an example which is used to explain my problem better. I hope you can help me.
我希望它成为:
This is the input text. It's very
boring to read because it's only
an example which is used to explain
my problem better. I hope you can
help me.
当然,我们需要考虑到你不能在一个单词的中间分开。当你有像'和 - 这样的符号时,它可能会变得非常棘手,所以我想知道是否有工具可以为你做到这一点?我听说过NLTK,但我还没有找到解决方案,也许这有点矫枉过正?
答案 0 :(得分:4)
这里有一个textwrap
库:
http://docs.python.org/2/library/textwrap.html
也可以在那里找到例子。您可能想要使用:
textwrap.fill(text, width)
答案 1 :(得分:0)
我现在也找到了在Django项目(我使用的)中执行此操作的首选方法。它是内置模板标记wordwrap
{{ value|wordwrap:5 }}
https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#wordwrap