Python extract sentence containing word

后端 未结 6 1706
孤独总比滥情好
孤独总比滥情好 2020-12-09 10:52

I am trying to extract all the sentence containing a specified word from a text.

txt=\"I like to eat apple. Me too. Let\'s go buy some apples.\"
txt = \".\"         


        
6条回答
  •  独厮守ぢ
    2020-12-09 11:32

    You can use str.split,

    >>> txt="I like to eat apple. Me too. Let's go buy some apples."
    >>> txt.split('. ')
    ['I like to eat apple', 'Me too', "Let's go buy some apples."]
    
    >>> [ t for t in txt.split('. ') if 'apple' in t]
    ['I like to eat apple', "Let's go buy some apples."]
    

提交回复
热议问题