【发布时间】:2019-01-24 11:50:05
【问题描述】:
我正在尝试使用 Geotext 提取字符串中的所有国家/地区
它适用于一些句子,但不适用于某些句子。
我尝试在 Python 3.6 中做到这一点。
s="India Vs Ireland T20 Series"
s=GeoText(s)
s.countries
预期结果:
['India','Ireland']
实际结果:
['Ireland']
【问题讨论】:
我正在尝试使用 Geotext 提取字符串中的所有国家/地区
它适用于一些句子,但不适用于某些句子。
我尝试在 Python 3.6 中做到这一点。
s="India Vs Ireland T20 Series"
s=GeoText(s)
s.countries
预期结果:
['India','Ireland']
实际结果:
['Ireland']
【问题讨论】:
您可以将 pycountry 用于您的任务(它也适用于 python 3):
pip install pycountry
import pycountry
text = "United States (New York), United Kingdom (London)"
for country in pycountry.countries:
# Handle both the cases(Uppercase/Lowercase)
if str(country.name).lower() in str(text).lower():
print country.name
【讨论】: