如何将python中的段落转换为双引号句子

How to convert paragraphs to double quotes sentences in python

我有一个包含很多段落的文本文件,我想按句子拆分它,即在每个点“.”之后。要么 ?它拆分并将句子包含在双 Qoutes 中,例如:

这是一个句子。这是激动人心的一句话!你认为这是一个问题吗?那又怎样.

"This is a sentence."

"This is an excited sentence!"

"And do you think this is a question?"

"so what then."

并将所有句子保存在文本文件中。

def splitParagraphIntoSentences(paragraph):

    import re
    sentenceEnders = re.compile('[.!?]')
    sentenceList = sentenceEnders.split(paragraph)
    return sentenceList

if __name__ == '__main__':
    p = """This is a sentence.  This is an excited sentence! And do you think this is a question? so what to do then because many people will say this ok. and then what ?"""

   sentences = splitParagraphIntoSentences(p)
   for s in sentences:
       sentence=(s.strip())
   file = open("another.txt", "w")
   file.write(sentence)
   file.close()

它不起作用,并且不确定如何将每个句子用双引号引起来,有帮助吗???

如果我理解正确,请尝试将您的代码修改为以下代码:

import re


def splitParagraphIntoSentences(paragraph):
    ''' break a paragraph into sentences
    and return a list '''

    sentenceEnders = re.compile('[.!?]')
    sentenceList = sentenceEnders.split(paragraph)
    return sentenceList

if __name__ == '__main__':
    p = "This is a sentence. This is an excited sentence! And do you think this is a question? so what to do then because many people will say this ok. and then what ?"

    sentences = splitParagraphIntoSentences(p)

    file = open('another.txt', "w")

    for s in sentences:
        if s.strip():
            file.write('"' + s.strip() + '"\n')  # Add a newline after each sentence

    file.close()

在你的情况下,你当然需要首先阅读文件而不是 p,因为你的(我猜)只是一个简化。