python - 检查输入的国家是否是世界上的国家之一

标签 python

是否有一种自动方法来检查输入的国家名称是否是 python 中的世界国家之一(即,是否有一种自动方法来获取世界所有国家/地区的列表?)

最佳答案

我知道 8 个月前就有人问过这个问题,但如果您来自 Google(就像我一样),这里有一个很好的解决方案。

您可以使用位于此处的 ISO 标准库:
https://pypi.python.org/pypi/iso3166/0.6
这段代码取自该链接,以防将来某个时间出现 404 错误:

Installation:

pip install iso3166

Country Details:

>>> from iso3166 import countries
>>> countries.get('us')
Country(name=u'United States', alpha2='US', alpha3='USA', numeric='840')
>>> countries.get('ala')
Country(name=u'\xc5land Islands', alpha2='AX', alpha3='ALA', numeric='248')
>>> countries.get(8)
Country(name=u'Albania', alpha2='AL', alpha3='ALB', numeric='008')

Countries List:

>>> from iso3166 import countries
>>> for c in countries:
>>>       print c
Country(name=u'Afghanistan', alpha2='AF', alpha3='AFG', numeric='004')
Country(name=u'\xc5land Islands', alpha2='AX', alpha3='ALA', numeric='248')
Country(name=u'Albania', alpha2='AL', alpha3='ALB', numeric='008')
Country(name=u'Algeria', alpha2='DZ', alpha3='DZA', numeric='012')
...


如果您想遵循 ISO 提出的标准化,则此软件包是兼容的。根据维基百科:

ISO 3166 is a standard published by the International Organization for Standardization (ISO) that defines codes for the names of countries, dependent territories, special areas of geographical interest, and their principal subdivisions (e.g., provinces or states). The official name of the standard is Codes for the representation of names of countries and their subdivisions.



因此,我强烈建议您在所有应用程序中使用此库,以防您与国家/地区合作。

希望这个数据对社区有用!

关于python - 检查输入的国家是否是世界上的国家之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41245330/

相关文章:

python - 加快阵列中所有可能对之间的距离

python - 检查 pandas 中某些列的值并创建属于该列的 ID 列表

python - 具有大数字的 Python ** 和 % 运算符的行为

python - 如何解决溢出错误: cannot convert float infinity to integer

python - 设置 pandas Dataframe Boxplot() 的 y 轴刻度,3 个偏差?

javascript - 对象的方法可以作用于自身吗?

python - 无法返回整个 CSV 数据框

python - 如何对齐 ruamel.yaml 中的 eol 注释,以便它们都位于同一列中

python - 有没有什么linux工具可以找到python程序的进程轨迹?

python - 如何在 Pandas 中实现 rolling_pct_change(period)?