Python中的语言代码列表(ISO639-1)?

use*_*838 5 python

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

以下不起作用我害怕:

import pycountry
pycountry.languages
Run Code Online (Sandbox Code Playgroud)

Rob*_*obᵩ 7

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

countries = [country.alpha2 for country in pycountry.countries]
Run Code Online (Sandbox Code Playgroud)

这将为您提供两位数的ISO639-1语言代码列表:

langs = [lang.iso639_1_code
         for lang in pycountry.languages
         if hasattr(lang, 'iso639_1_code')]
Run Code Online (Sandbox Code Playgroud)

这将为您提供所有语言代码的列表:

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]
Run Code Online (Sandbox Code Playgroud)

  • 它是 *alpha_2*,至少在 Python 3 上是这样。 (4认同)