How to extract countries from a text?
you could use pycountry for your task (it also works with python 3):
pip install pycountry
import pycountry
text = "United States (New York), United Kingdom (London)"
for country in pycountry.countries:
if country.name in text:
print(country.name)
There is a newer version for this library that supports python3 named geograpy3
pip install geograpy3
It allows you to extract place names from a URL or text, and add context to those names -- for example distinguishing between a country, region or city.
Example:
import geograpy
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('maxent_ne_chunker')
nltk.download('words')
url = 'http://www.bbc.com/news/world-europe-26919928'
places = geograpy.get_place_context(url=url)
You can find more details under this link: