0
我試圖將國家/地區名稱轉換爲所需的國家/地區代碼。從用戶輸入中使用pycountry獲取國家代碼
例如:
United Kingdom : UK
我已經嘗試以下操作:
import pycountry
user_input = raw_input(': ')
mapping = {country.name: country.alpha2 for country in pycountry.countries}
print mapping.get(user_input)
我相信我可能誤解了的文檔,因爲我收到以下錯誤:
mapping = {country.name: country.alpha2 for country in pycountry.countries}
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pycountry/db.py", line 22, in __getattr__
raise AttributeError
AttributeError
應該是'country.alpha_2'而不是'country.alpha2'。 – acw1668