我在一个hmd.txt文件中有许多句子,我想从整个文本文件中用“”将每个句子分成一段。例如

2024-06-16 12:30:15 发布

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

“我无法在这三款相机(以及它们的变种索尼HX30和松下ZX20)之间做出决定,因为每款相机的许多评论都让它们听起来非常相似。所以,我出去买了(从接受退货的商人那里)这三个相机中的每一个。然后我在各种条件下拍摄了照片和视频。我不是专业摄影师,我也没有对图像进行Imatest或任何其他特定测试(你可以阅读C/net),但这是一个普通人的经历,他用这三台相机并排拍摄照片和视频,这是你不常看到的比较。”

“归根结底,这三款相机非常相似,但也有一些细微的差别。但这些差别将它们区分开来,可能会让你决定更喜欢其中一款。”

“一般来说,这些相机都不是DSLR的替代品。不管是谁写的照片和单反相机一样好,他说的都不准确。而且,没有一款相机可以替代高端摄像机。”

我需要的输出是

  1. “我无法在这三款相机(以及它们的变种索尼HX30和松下ZX20)之间做出决定,因为每款相机的许多评论都让它们听起来非常相似。”
    1. “所以,我出去(从接受退货的商家那里)买了这三台相机各一台。”
  2. “然后我在各种条件下拍摄了照片和视频。”……等等

Tags: 图像视频net专业评论条件照片普通人
2条回答

我不明白,你问的句子,然后张贴代码分裂的话?我是不是误会了什么,我相信你在寻找这样的东西:

text=“”[复制粘贴示例文本]“”

for possible_paragraph in text.split("\n"):
    if len(possible_paragraph) != 0:
        paragraphs.append(possible_paragraph)
        sentences.append([])
        for sentence in paragraphs[-1].split(". "):
            if len(sentence) != 0:
                sentences[-1].append(sentence)

最终结果如下

>>> sentences[0]
[prints out first paragraph from "I couldn't decide..." to "which is a comparison you don't often see]
>>> sentences[1]
[prints out the 2nd paragraph "The bottom line is..."]

你得到了分句,也就是第一段的第一句:

>>> sentences[0][0]
'"I couldn\'t decide between these three cameras (and their variants, the Sony HX30 and the Panasonic ZX20) because many of the reviews of each made the cameras sound very similar'

第二句第一段

>>> sentences[0][1]
'So, I went out and bought (from merchants who accepted returns) one of each of these three cameras'

第一句第二段

>>> sentences[1][0]
'"The bottom line is that these three cameras are very similar with a few minor differences.But those differences set them apart and may make you decide that you like one more than the other."'

等等

“然后我拿了……”不是第二段,因为它和前一句之间没有输入,或者是因为你的格式不好(不是会计所以呈现),或者是我对你需要什么/段落是什么的误解

>>>import re
# reading a file and reading line in it 
>>>text = ''.join(open('file.txt').readlines())
# reading sentences by separating them 
>>>sentences = re.split(r' *[][]* *', text)
>>>print (sentences)

现在这个代码是分开句子的

相关问题 更多 >