2016-12-20 77 views
0

有沒有一種自動化的方法來檢查輸入的國家名是否是Python中世界的國家之一(即,是否有自動獲取列表的方式?世界上所有的國家檢查一個國家是否是世界上其中一個國家

+0

你是否搜索了「python國家名單」? –

+0

許多方法可以做到這一點。試試谷歌。 – Inconnu

+1

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

回答

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

你的情況下,有關於每個國家的更多的數據,你需要它,你可以在被請求的代碼的鏈接檢查

0

您可以使用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', 'Hong Kong'), 
    ('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', 'Mongolia'), 
    ('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', 'Taiwan'), 
    ('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。 –

0

這是使用從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', 'hong kong', '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', 'mongolia', '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', 'taiwan (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 
0

我知道這已被要求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)的定義爲國家,屬地的名稱,地域利益的特殊區域的代碼,以及其主要細分發表的標準(例如,省或州)。該標準的正式名稱是代碼,用於表示國家及其分支的名稱。

因此,我強烈建議在所有的應用程序中使用這個庫,以防您正在使用國家。

希望這條數據對社區有用!

相關問題