在一定数量的句号后给双换行符,使字符串分成段落

2024-04-29 11:02:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中有一个长字符串

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)."

我想在一定数量的句号后加上两行新行,使字符串分成段落

我尝试了textwrap,但不确定textwrap是否成功。你知道如何做到这一点吗

预期输出如下所示:

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).

Tags: andofthebythatisaspage
2条回答

请尝试以下代码:

`s = '''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)'''

a = ''
for i in s:
    if i=='.':
        a+=i
        a+=' \n\n'
    else:
        a+=i
print(a)`

如果您执行了代码,您就会意识到输出并不完全是您所需要的,因为每行的开头都有一个空格。这是因为您输入的字符串。周期之后的空白空间使得线条从边缘进一步显示出1个空间。因此,请删除句点后的空格,您将获得所需的输出。如果有一种方法可以在不删除字符串中句点后的空格的情况下获得所需的输出,请告诉我

如果字符串数据必须在代码中,我建议使用多行字符串,我们也需要使用textwrap.dedent,以防多行字符串导致缩进。然后我们可以删除我们不想要的换行符,然后我们可以用一个句号+换行符替换句号

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)

您还可以选择将字符串数据放在一个单独的文本文件中,因为该字符串相当长,这意味着您可以避免更多的字符串操作,同时保持干净的代码

myfile = open('file.txt', 'r')
longString = myfile.read().replace('. ', '.\n\n')
print(longString)

相关问题 更多 >