Reputation: 3647
I have a text paragraph my_text
which contains lines like
........ licensed in Bangladesh. We are happy to announce ......................
................................................
I want to extract the word "Bangladesh" from it. To decide if i want that word depends upon the presence of "licensed in" in the sentence.
Current code is like:
texts = my_text.split("licensed in")
# extract the word before the first dot (.) from texts[1]
What is more appropriate way to do it in python?
Upvotes: 0
Views: 107
Reputation: 1971
How about
>>> my_text.split('licensed in ')[1].split('.')[0]
'Bangladesh'
Upvotes: 0
Reputation: 336158
That's a job for a regex:
import re
location = re.search(r"licensed in ([^.]*)", my_text).group(1)
Explanation:
licensed\ in\ # Match "licensed in "
( # Match and capture in group 1:
[^.]* # Any number of characters except dots.
) # End of capturing group 1
Upvotes: 2