I am trying to remove whole sentences that start with a certain phrase, but I want to retain the rest of the body text. For example:
text = "Hello I like dogs. I also like cats. Hello I like animals"
I want to remove any sentence that starts with "Hello" But retain the rest, therefore the function should only leave:
"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:
if v[0:5].casefold() == 'hello':
It refers to the string as lowercase.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.