擅长:python、mysql、java
<p>为了使它看起来像报纸,你应该使用一个适当的连字算法。
<a href="https://pypi.org/project/PyHyphen/" rel="nofollow noreferrer">PyHyphen</a>库包含libreoffice中使用的断字字典,并支持多种语言(默认语言为<code>en_US</code>)。你知道吗</p>
<pre><code># pip install pyhyphen
from hyphen import Hyphenator
from textwrap2 import wrap
english = Hyphenator('en_US')
print('\n'.join(wrap(lorem_text, width=20, use_hyphenator=english)))
</code></pre>
<p>输出将如下所示。请注意,有些行短于20个字符。连字符仅用于长单词,并遵循特定于语言的连字符规则。你知道吗</p>
<pre>
Contrary to popular
belief, Lorem Ipsum
is not simply random
text. It has roots
in a piece of clas-
sical Latin litera-
ture from 45 BC,
making it over 2000
years old.
</pre>