如何提取Country 物件中的值或轉換為可迭代物件?
也許特別是這是不可能的。
目標:使用alpha_2引數查找國家代碼并提取所有值。
用法
pip install pycountry
import pycountry
pycountry.countries.get(alpha_2='DE')
>>> Country(alpha_2='DE', alpha_3='DEU', flag='????', name='Germany', numeric='276', official_name='Federal Republic of Germany')
試圖
list(pycountry.countries.get(alpha_2='DE'))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-38-5d8d83cf3b5c> in <module>
----> 1 list(pycountry.countries.get(alpha_2='DE'))
TypeError: 'Country' object is not iterable
uj5u.com熱心網友回復:
get()是正確的,但您不會將其轉換為串列。您可以.get()使用已知屬性獲取國家/地區,然后使用點表示法獲取其他屬性。例如:country = pycountry.countries.get(alpha_2='DE')然后country.name將為您提供“德國”,country.alpha_3將為您提供德國的 alpha3 代碼(即“DEU”)等。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/443862.html
標籤:python-3.x 类型错误 pycountry转换
