是否可以从pycountry 1.15获取所有ISO639-1语言代码的列表?例如,['en','it','el','fr',...]?如果是,那怎么办?

恐怕以下操作无效:

import pycountry
pycountry.languages

最佳答案

这将为您提供两位数字的ISO3166-1国家/地区代码列表:

countries = [country.alpha2 for country in pycountry.countries]
#countries = [country.alpha_2 for country in pycountry.countries]  # for python3
这将为您提供两位数字的ISO639-1语言代码列表:
langs = [lang.iso639_1_code
         for lang in pycountry.languages
         if hasattr(lang, 'iso639_1_code')]
这将为您提供所有语言代码的列表:
langs = [getattr(lang, 'iso639_1_code', None) or
         getattr(lang, 'iso639_2T_code', None) or
         getattr(lang, 'iso639_3_code')
         for lang in pycountry.languages]

09-28 14:39