本文介绍了如何从文本中提取国家?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Python 3(我也安装了 Python 2)并且我想从短文本中提取国家或城市.例如,text = "I live in Spain"text = "United States (New York), United Kingdom (London)".

I use Python 3 (I also have Python 2 installed) and I want to extract countries or cities from a short text.For example, text = "I live in Spain" or text = "United States (New York), United Kingdom (London)".

各国的答案:

  1. 西班牙
  2. [美国、英国]

我尝试安装 geography,但无法运行 pip install geography.我收到此错误:

I tried to install geography but I am unable to run pip install geography. I get this error:

正在收集地理 找不到满足需求地理(来自版本:)未找到匹配的分布地理

看起来 geography 只适用于 Python 2.

It looks like geography only works with Python 2.

我也有 geopandas,但我不知道如何使用 geopandas 从文本中提取所需的信息.

I also have geopandas, but I don't know how to extract the required info from text using geopandas.

推荐答案

你可以使用 pycountry 来完成你的任务(它也适用于 python 3):

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)

这篇关于如何从文本中提取国家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 11:52