是否有一种自动方法来检查输入的国家名称是否是 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/