擅长:python、mysql、java
<p>使用<code>re</code>模块,<a href="https://kite.com/python/docs/re.sub" rel="nofollow noreferrer">^{<cd2>}</a>函数将允许您这样做。
我们需要用单个的<code>\n</code>替换multilpe<code>\n</code>出现,并删除<code> </code>字符串</p>
<pre><code>import re
code='''Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea let's do more of those!'''
result = re.sub('\n{2,}', '\n', code)
result = re.sub(' ', ' ', result)
print(result)
</code></pre>
<p>然后分割你的文本。在</p>