简体   繁体   English

Python:如何删除以特定单词开头的句子

[英]Python: How to remove sentences starting with a specific word(s)

I am trying to remove whole sentences that start with a certain phrase, but I want to retain the rest of the body text.我正在尝试删除以某个短语开头的整个句子,但我想保留正文的 rest。 For example:例如:

text = "Hello I like dogs. I also like cats. Hello I like animals" text = "你好,我喜欢狗。我也喜欢猫。你好,我喜欢动物"

I want to remove any sentence that starts with "Hello" But retain the rest, therefore the function should only leave:我想删除任何以“Hello”开头的句子,但保留 rest,因此 function 应该只留下:

"I also like cats." “我也喜欢猫。”

Currently I am experimenting with regex expressions, but I am unsure of a way to achieve this.目前我正在尝试使用正则表达式,但我不确定实现这一点的方法。 Any help would be appreciated.任何帮助,将不胜感激。

Here is a basic approach.这是一个基本的方法。 You may need to use something more fancy in order to split the sentences;您可能需要使用更花哨的东西来拆分句子; see this post for more details.有关更多详细信息,请参阅此帖子

>>> text = "Hello I like dogs. I also like cats. Hello I like animals"
>>> sentences = text.split(". ")
>>> ". ".join(s for s in sentences if not s.lower().startswith("hello")) + "."
'I also like cats.'

read the code notes plaese:请阅读代码注释:

text = "Hello I like dogs. I also like cats. Hello I like animals"
#get list of sentences, split by the DOT and space ". "
#like: ['Hello I like dogs', 'I also like cats', 'Hello I like animals']
t = text.split('. ')
#now lets loop for each value on our list
for v in t:
    #check if the first 5 letters are "Hello"
    if v[0:5] == 'Hello':
        #if it is - remove the value from the list.
        t.remove(v)
#now we have list of filtered strings:
#t

notice that the word 'Hello' may UPPER/LOWER case, so if you want to cover it all, add at the if:请注意,“Hello”这个词可能是大写/小写,所以如果你想涵盖所有内容,请在 if 处添加:

if v[0:5].casefold() == 'hello':

It refers to the string as lowercase.它将字符串称为小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python删除以特定单词开头的所有行 - Python Remove all Lines starting with specific word 在Python中查找特定单词的句子索引(列表中的句子) - Find the sentence’s index (sentences in a list) of a specific word in Python 如何在 python 的段落句子中设置字数限制? - how set a word limit in paragraph's sentences in python? 如何删除具有特定字符的句子? - How to remove sentences with a specific character? 如何从“关键字” python开始替换特定行中的特定单词 - How to replace a specific word in specific line starting with a “keyword” python 如何用python有效替换word文档中的句子 - How to effectively replace sentences in word document with python 如何从 R 或 Python 上带有特定单词列表的文本文件中过滤出句子? - How can I filter out sentences from a text file with specific word list on R or Python? 如何删除以python中的特定单词开头的重复行 - How to delete repeating lines starting with specific word in python 如何使用 Python NLP 从句子列表中提取特定单词。 这些词是医疗设备的零件 - How to extract particular word(s) from the list of sentences using Python NLP. These word(s) are Parts of Medical equipments 如何找到以大写字母开头的任何单词包围的特定预定义单词? - How to find a specific, pre-defined word surrounded by any word(s) starting with a capital letter(s)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM