【问题标题】:Separate string paragraph with sentence用句子分隔字符串段落
【发布时间】:2019-08-07 09:51:24
【问题描述】:

我正在尝试使用段落分隔字符串(即实际上它们是电子邮件中的线程),并且我需要根据字符串的句子而不是单词来拆分字符串。

我试过的是:

def split_text_from_sentence(text, sentence):
    chopped_text = re.split(sentence, text)
    return chopped_text

我得到了什么:

full_text = ¨Good morning Carlos. We leave you attached the excel for info. Regards. Luis Miguel. Company Name blablabla From: Oscar Herrero, Carlos Sent: tuesday 25 of octoberFor: Administration Matter: [INTERNAL] Good afternoon, I need to ask you for a requirement to add in Sharepoint. Un saludo, Carlos OSCAR HERRERO engineering Support T8 1st floor | carlos.oscar-herrero@blabla.com¨

signature_text = ¨From: Oscar Herrero, Carlos Sent: tuesday 25 of octoberFor: Administration Matter: [INTERNAL]¨

我正在寻找类似的东西:

first_message = ¨Good morning Carlos. We leave you attached the excel for info. Regards. Luis Miguel. Company Name blablabla ¨

rest_of_message = ¨Good afternoon, I need to ask you for a requirement to add in Sharepoint. Un saludo, Carlos OSCAR HERRERO engineering Support T8 1st floor | carlos.oscar-herrero@blabla.com¨

【问题讨论】:

    标签: regex python-3.x string split


    【解决方案1】:
    def split_text_from_sentence(text, sentence):
        chopped_text = text.split(sentence)
        return chopped_text
    
    full_text = "Good morning Carlos. We leave you attached the excel for info. Regards. Luis Miguel. Company Name blablabla From: Oscar Herrero, Carlos Sent: tuesday 25 of octoberFor: Administration Matter: [INTERNAL] Good afternoon, I need to ask you for a requirement to add in Sharepoint. Un saludo, Carlos OSCAR HERRERO engineering Support T8 1st floor | carlos.oscar-herrero@blabla.com"
    signature_text = "From: Oscar Herrero, Carlos Sent: tuesday 25 of octoberFor: Administration Matter: [INTERNAL]"
    first_message,rest_of_the_message= split_text_from_sentence(full_text,signature_text)
    print(first_message,rest_of_the_message)
    

    如果可以使用字符串方法来完成,为什么还要使用“re”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 2013-06-04
      • 1970-01-01
      • 2015-03-14
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多