<p>如果字符串数据必须在代码中,我建议使用多行字符串,我们也需要使用textwrap.dedent,以防多行字符串导致缩进。然后我们可以删除我们不想要的换行符,然后我们可以用一个句号+换行符替换句号</p>
<pre><code>import textwrap
longString = """It is a long established fact that a reader will be
distracted by the readable content of a page when looking at its layout.
The point of using Lorem Ipsum is that it has a more-or-less normal
distribution of letters, as opposed to using 'Content here, content
here', making it look like readable English. Many desktop publishing
packages and web page editors now use Lorem Ipsum as their default model
text, and a search for 'lorem ipsum' will uncover many web sites still
in their infancy. Various versions have evolved over the years,
sometimes by accident, sometimes on purpose (injected humour and the
like)."""
longString = textwrap.dedent(longString)
longString = longString.replace('\n', '')
longString = longString.replace('. ', '.\n\n')
print(longString)
</code></pre>
<p>您还可以选择将字符串数据放在一个单独的文本文件中,因为该字符串相当长,这意味着您可以避免更多的字符串操作,同时保持干净的代码</p>
<pre><code>myfile = open('file.txt', 'r')
longString = myfile.read().replace('. ', '.\n\n')
print(longString)
</code></pre>