检查一个国家是否是世界上其中一个国家

问题描述:

有没有一种自动化的方法来检查输入的国家名是否是Python中世界的国家之一(即,是否有自动获取列表的方式?世界上所有的国家检查一个国家是否是世界上其中一个国家

+0

你是否搜索了“python国家名单”? –

+0

许多方法可以做到这一点。试试谷歌。 – Inconnu

+1

https://pypi.python.org/pypi/pycountry/16.11.27.1可能是你需要的。 –

的),你可以不喜欢它:

import requests 

req = requests.get('https://raw.githubusercontent.com/Miguel-Frazao/world-data/master/countries_data.json').json() 
countries = (i['name'] for i in req) 
print(list(countries)) 

您可以将它们保存到一个文件,这样你就不必做要求所有的时间,或者只是复制粘贴到您的代码中。

然后,检查该国存在,你可以这样做:

... 
country = input('Insert a country') 
if country not in countries: 
    print('nice try, but invalid') 
else: 
    print('yooo, your country is {}'.format(country)) 

你的情况下,有关于每个国家的更多的数据,你需要它,你可以在被请求的代码的链接检查

您可以使用pycountry库获得所有国家的列表:

pip install pycountry 

或者您可以使用这本字典:

Country = [ 
    ('US', 'United States'), 
    ('AF', 'Afghanistan'), 
    ('AL', 'Albania'), 
    ('DZ', 'Algeria'), 
    ('AS', 'American Samoa'), 
    ('AD', 'Andorra'), 
    ('AO', 'Angola'), 
    ('AI', 'Anguilla'), 
    ('AQ', 'Antarctica'), 
    ('AG', 'Antigua And Barbuda'), 
    ('AR', 'Argentina'), 
    ('AM', 'Armenia'), 
    ('AW', 'Aruba'), 
    ('AU', 'Australia'), 
    ('AT', 'Austria'), 
    ('AZ', 'Azerbaijan'), 
    ('BS', 'Bahamas'), 
    ('BH', 'Bahrain'), 
    ('BD', 'Bangladesh'), 
    ('BB', 'Barbados'), 
    ('BY', 'Belarus'), 
    ('BE', 'Belgium'), 
    ('BZ', 'Belize'), 
    ('BJ', 'Benin'), 
    ('BM', 'Bermuda'), 
    ('BT', 'Bhutan'), 
    ('BO', 'Bolivia'), 
    ('BA', 'Bosnia And Herzegowina'), 
    ('BW', 'Botswana'), 
    ('BV', 'Bouvet Island'), 
    ('BR', 'Brazil'), 
    ('BN', 'Brunei Darussalam'), 
    ('BG', 'Bulgaria'), 
    ('BF', 'Burkina Faso'), 
    ('BI', 'Burundi'), 
    ('KH', 'Cambodia'), 
    ('CM', 'Cameroon'), 
    ('CA', 'Canada'), 
    ('CV', 'Cape Verde'), 
    ('KY', 'Cayman Islands'), 
    ('CF', 'Central African Rep'), 
    ('TD', 'Chad'), 
    ('CL', 'Chile'), 
    ('CN', 'China'), 
    ('CX', 'Christmas Island'), 
    ('CC', 'Cocos Islands'), 
    ('CO', 'Colombia'), 
    ('KM', 'Comoros'), 
    ('CG', 'Congo'), 
    ('CK', 'Cook Islands'), 
    ('CR', 'Costa Rica'), 
    ('CI', 'Cote D`ivoire'), 
    ('HR', 'Croatia'), 
    ('CU', 'Cuba'), 
    ('CY', 'Cyprus'), 
    ('CZ', 'Czech Republic'), 
    ('DK', 'Denmark'), 
    ('DJ', 'Djibouti'), 
    ('DM', 'Dominica'), 
    ('DO', 'Dominican Republic'), 
    ('TP', 'East Timor'), 
    ('EC', 'Ecuador'), 
    ('EG', 'Egypt'), 
    ('SV', 'El Salvador'), 
    ('GQ', 'Equatorial Guinea'), 
    ('ER', 'Eritrea'), 
    ('EE', 'Estonia'), 
    ('ET', 'Ethiopia'), 
    ('FK', 'Falkland Islands (Malvinas)'), 
    ('FO', 'Faroe Islands'), 
    ('FJ', 'Fiji'), 
    ('FI', 'Finland'), 
    ('FR', 'France'), 
    ('GF', 'French Guiana'), 
    ('PF', 'French Polynesia'), 
    ('TF', 'French S. Territories'), 
    ('GA', 'Gabon'), 
    ('GM', 'Gambia'), 
    ('GE', 'Georgia'), 
    ('DE', 'Germany'), 
    ('GH', 'Ghana'), 
    ('GI', 'Gibraltar'), 
    ('GR', 'Greece'), 
    ('GL', 'Greenland'), 
    ('GD', 'Grenada'), 
    ('GP', 'Guadeloupe'), 
    ('GU', 'Guam'), 
    ('GT', 'Guatemala'), 
    ('GN', 'Guinea'), 
    ('GW', 'Guinea-bissau'), 
    ('GY', 'Guyana'), 
    ('HT', 'Haiti'), 
    ('HN', 'Honduras'), 
    ('HK', '*'), 
    ('HU', 'Hungary'), 
    ('IS', 'Iceland'), 
    ('IN', 'India'), 
    ('ID', 'Indonesia'), 
    ('IR', 'Iran'), 
    ('IQ', 'Iraq'), 
    ('IE', 'Ireland'), 
    ('IL', 'Israel'), 
    ('IT', 'Italy'), 
    ('JM', 'Jamaica'), 
    ('JP', 'Japan'), 
    ('JO', 'Jordan'), 
    ('KZ', 'Kazakhstan'), 
    ('KE', 'Kenya'), 
    ('KI', 'Kiribati'), 
    ('KP', 'Korea (North)'), 
    ('KR', 'Korea (South)'), 
    ('KW', 'Kuwait'), 
    ('KG', 'Kyrgyzstan'), 
    ('LA', 'Laos'), 
    ('LV', 'Latvia'), 
    ('LB', 'Lebanon'), 
    ('LS', 'Lesotho'), 
    ('LR', 'Liberia'), 
    ('LY', 'Libya'), 
    ('LI', 'Liechtenstein'), 
    ('LT', 'Lithuania'), 
    ('LU', 'Luxembourg'), 
    ('MO', 'Macau'), 
    ('MK', 'Macedonia'), 
    ('MG', 'Madagascar'), 
    ('MW', 'Malawi'), 
    ('MY', 'Malaysia'), 
    ('MV', 'Maldives'), 
    ('ML', 'Mali'), 
    ('MT', 'Malta'), 
    ('MH', 'Marshall Islands'), 
    ('MQ', 'Martinique'), 
    ('MR', 'Mauritania'), 
    ('MU', 'Mauritius'), 
    ('YT', 'Mayotte'), 
    ('MX', 'Mexico'), 
    ('FM', 'Micronesia'), 
    ('MD', 'Moldova'), 
    ('MC', 'Monaco'), 
    ('MN', '*'), 
    ('MS', 'Montserrat'), 
    ('MA', 'Morocco'), 
    ('MZ', 'Mozambique'), 
    ('MM', 'Myanmar'), 
    ('NA', 'Namibia'), 
    ('NR', 'Nauru'), 
    ('NP', 'Nepal'), 
    ('NL', 'Netherlands'), 
    ('AN', 'Netherlands Antilles'), 
    ('NC', 'New Caledonia'), 
    ('NZ', 'New Zealand'), 
    ('NI', 'Nicaragua'), 
    ('NE', 'Niger'), 
    ('NG', 'Nigeria'), 
    ('NU', 'Niue'), 
    ('NF', 'Norfolk Island'), 
    ('MP', 'Northern Mariana Islands'), 
    ('NO', 'Norway'), 
    ('OM', 'Oman'), 
    ('PK', 'Pakistan'), 
    ('PW', 'Palau'), 
    ('PA', 'Panama'), 
    ('PG', 'Papua New Guinea'), 
    ('PY', 'Paraguay'), 
    ('PE', 'Peru'), 
    ('PH', 'Philippines'), 
    ('PN', 'Pitcairn'), 
    ('PL', 'Poland'), 
    ('PT', 'Portugal'), 
    ('PR', 'Puerto Rico'), 
    ('QA', 'Qatar'), 
    ('RE', 'Reunion'), 
    ('RO', 'Romania'), 
    ('RU', 'Russian Federation'), 
    ('RW', 'Rwanda'), 
    ('KN', 'Saint Kitts And Nevis'), 
    ('LC', 'Saint Lucia'), 
    ('VC', 'St Vincent/Grenadines'), 
    ('WS', 'Samoa'), 
    ('SM', 'San Marino'), 
    ('ST', 'Sao Tome'), 
    ('SA', 'Saudi Arabia'), 
    ('SN', 'Senegal'), 
    ('SC', 'Seychelles'), 
    ('SL', 'Sierra Leone'), 
    ('SG', 'Singapore'), 
    ('SK', 'Slovakia'), 
    ('SI', 'Slovenia'), 
    ('SB', 'Solomon Islands'), 
    ('SO', 'Somalia'), 
    ('ZA', 'South Africa'), 
    ('ES', 'Spain'), 
    ('LK', 'Sri Lanka'), 
    ('SH', 'St. Helena'), 
    ('PM', 'St.Pierre'), 
    ('SD', 'Sudan'), 
    ('SR', 'Suriname'), 
    ('SZ', 'Swaziland'), 
    ('SE', 'Sweden'), 
    ('CH', 'Switzerland'), 
    ('SY', 'Syrian Arab Republic'), 
    ('TW', '*'), 
    ('TJ', 'Tajikistan'), 
    ('TZ', 'Tanzania'), 
    ('TH', 'Thailand'), 
    ('TG', 'Togo'), 
    ('TK', 'Tokelau'), 
    ('TO', 'Tonga'), 
    ('TT', 'Trinidad And Tobago'), 
    ('TN', 'Tunisia'), 
    ('TR', 'Turkey'), 
    ('TM', 'Turkmenistan'), 
    ('TV', 'Tuvalu'), 
    ('UG', 'Uganda'), 
    ('UA', 'Ukraine'), 
    ('AE', 'United Arab Emirates'), 
    ('UK', 'United Kingdom'), 
    ('UY', 'Uruguay'), 
    ('UZ', 'Uzbekistan'), 
    ('VU', 'Vanuatu'), 
    ('VA', 'Vatican City State'), 
    ('VE', 'Venezuela'), 
    ('VN', 'Viet Nam'), 
    ('VG', 'Virgin Islands (British)'), 
    ('VI', 'Virgin Islands (U.S.)'), 
    ('EH', 'Western Sahara'), 
    ('YE', 'Yemen'), 
    ('YU', 'Yugoslavia'), 
    ('ZR', 'Zaire'), 
    ('ZM', 'Zambia'), 
    ('ZW', 'Zimbabwe') 
] 
+0

字典是过时的已经,例如没有南斯拉夫了,塞尔维亚,黑山丢失,南苏丹丢失,扎伊尔被称为刚果*Reublic,捷克*有一个很短的名字今年以来:捷克,几内亚比绍应被拼写几内亚比绍,我相信还有更多的错误 –

+0

这是正确的,但你可以编辑代码,如果需要的话。 – Inconnu

+0

是的,你可以编辑,但你不记得所有的改变。我会推荐这个https://www.iso.org/obp/ui/#search/code/。但是没有API afaik。 –

这是使用从https://www.iso.org/obp/ui/#search收集到的国家名称粗的开始。国名还包含一些棘手的案例。例如,这段代码承认'萨摩亚',但它并不真正'看到''美属萨摩亚'。

class Countries: 
    def __init__(self): 
     self.__countries = ['afghanistan', 'aland islands', 'albania', 'algeria', 'american samoa', 'andorra', 'angola', 'anguilla', 'antarctica', 'antigua and barbuda', 'argentina', 'armenia', 'aruba', 'australia', 'austria', 'azerbaijan', 'bahamas (the)', 'bahrain', 'bangladesh', 'barbados', 'belarus', 'belgium', 'belize', 'benin', 'bermuda', 'bhutan', 'bolivia (plurinational state of)', 'bonaire, sint eustatius and saba', 'bosnia and herzegovina', 'botswana', 'bouvet island', 'brazil', 'british indian ocean territory (the)', 'brunei darussalam', 'bulgaria', 'burkina faso', 'burundi', 'cabo verde', 'cambodia', 'cameroon', 'canada', 'cayman islands (the)', 'central african republic (the)', 'chad', 'chile', 'china', 'christmas island', 'cocos (keeling) islands (the)', 'colombia', 'comoros (the)', 'congo (the democratic republic of the)', 'congo (the)', 'cook islands (the)', 'costa rica', "cote d'ivoire", 'croatia', 'cuba', 'curacao', 'cyprus', 'czechia', 'denmark', 'djibouti', 'dominica', 'dominican republic (the)', 'ecuador', 'egypt', 'el salvador', 'equatorial guinea', 'eritrea', 'estonia', 'ethiopia', 'falkland islands (the) [malvinas]', 'faroe islands (the)', 'fiji', 'finland', 'france', 'french guiana', 'french polynesia', 'french southern territories (the)', 'gabon', 'gambia (the)', 'georgia', 'germany', 'ghana', 'gibraltar', 'greece', 'greenland', 'grenada', 'guadeloupe', 'guam', 'guatemala', 'guernsey', 'guinea', 'guinea-bissau', 'guyana', 'haiti', 'heard island and mcdonald islands', 'holy see (the)', 'honduras', '*', 'hungary', 'iceland', 'india', 'indonesia', 'iran (islamic republic of)', 'iraq', 'ireland', 'isle of man', 'israel', 'italy', 'jamaica', 'japan', 'jersey', 'jordan', 'kazakhstan', 'kenya', 'kiribati', "korea (the democratic people's republic of)", 'korea (the republic of)', 'kuwait', 'kyrgyzstan', "lao people's democratic republic (the)", 'latvia', 'lebanon', 'lesotho', 'liberia', 'libya', 'liechtenstein', 'lithuania', 'luxembourg', 'macao', 'macedonia (the former yugoslav republic of)', 'madagascar', 'malawi', 'malaysia', 'maldives', 'mali', 'malta', 'marshall islands (the)', 'martinique', 'mauritania', 'mauritius', 'mayotte', 'mexico', 'micronesia (federated states of)', 'moldova (the republic of)', 'monaco', '*', 'montenegro', 'montserrat', 'morocco', 'mozambique', 'myanmar', 'namibia', 'nauru', 'nepal', 'netherlands (the)', 'new caledonia', 'new zealand', 'nicaragua', 'niger (the)', 'nigeria', 'niue', 'norfolk island', 'northern mariana islands (the)', 'norway', 'oman', 'pakistan', 'palau', 'palestine, state of', 'panama', 'papua new guinea', 'paraguay', 'peru', 'philippines (the)', 'pitcairn', 'poland', 'portugal', 'puerto rico', 'qatar', 'reunion', 'romania', 'russian federation (the)', 'rwanda', 'saint barthelemy', 'saint helena, ascension and tristan da cunha', 'saint kitts and nevis', 'saint lucia', 'saint martin (french part)', 'saint pierre and miquelon', 'saint vincent and the grenadines', 'samoa', 'san marino', 'sao tome and principe', 'saudi arabia', 'senegal', 'serbia', 'seychelles', 'sierra leone', 'singapore', 'sint maarten (dutch part)', 'slovakia', 'slovenia', 'solomon islands', 'somalia', 'south africa', 'south georgia and the south sandwich islands', 'south sudan', 'spain', 'sri lanka', 'sudan (the)', 'suriname', 'svalbard and jan mayen', 'swaziland', 'sweden', 'switzerland', 'syrian arab republic', '* (province of china)', 'tajikistan', 'tanzania, united republic of', 'thailand', 'timor-leste', 'togo', 'tokelau', 'tonga', 'trinidad and tobago', 'tunisia', 'turkey', 'turkmenistan', 'turks and caicos islands (the)', 'tuvalu', 'uganda', 'ukraine', 'united arab emirates (the)', 'united kingdom of great britain and northern ireland (the)', 'united states minor outlying islands (the)', 'united states of america (the)', 'uruguay', 'uzbekistan', 'vanuatu', 'venezuela (bolivarian republic of)', 'viet nam', 'virgin islands (british)', 'virgin islands (u.s.)', 'wallis and futuna', 'western sahara*', 'yemen', 'zambia', 'zimbabwe'] 

    def __call__(self, name, strict=3): 
     result = False 
     name = name.lower() 
     if strict==3: 
      for country in self.__countries: 
       if country==name: 
        return True 
      else: 
       return result 
     elif strict==2: 
      for country in self.__countries: 
       if name in country: 
        return True 
      else: 
       return result 
     elif strict==1: 
      for country in self.__countries: 
       if country.startswith(name): 
        return True 
      else: 
       return result 
     else: 
      return result 

countries = Countries() 
print (countries('germany')) 
print (countries('russia')) 
print (countries('russia', strict=2)) 
print (countries('russia', strict=1)) 
print (countries('samoa', strict=2)) 
print (countries('samoa', strict=1)) 

下面是结果:

True 
False 
True 
True 
True 
True 

我知道这已被要求8个月前,但这里是如果你是从谷歌来了(就像我)一个不错的解决方案。

可以使用ISO标准库位于: https://pypi.python.org/pypi/iso3166/0.6 这段代码是从的情况下,该链接采取你得到一个404错误一些在未来的时间:

安装:

pip install iso3166 

国家联系方式:

>>> 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') 

国家名单:

>>> 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是由国际标准化组织(ISO)的定义为国家,属地的名称,地域利益的特殊区域的代码,以及其主要细分发表的标准(例如,省或州)。该标准的正式名称是代码,用于表示国家及其分支的名称。

因此,我强烈建议在所有的应用程序中使用这个库,以防您正在使用国家。

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