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

标签 python

是否可以从 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]

关于python - Python 中的语言代码列表(ISO639-1)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32773035/

相关文章:

python - 检查 Pandas 数据框中的下一个索引列值和相同值的连续长度

python - 如何使用 oauth2 为网站构建 Python 爬虫

python mock.mock_reset() 返回模拟而不是重置模拟

使用 SSL 连接的 Python IBM_DB

python pandas dataframe 聚合行

python - 为像 numpy 这样的库创建 Python Wheels

Python 和 GCC 版本

python - 如何通过函数将字典中的多个键与一个值配对?

python - 在numpy中过滤掉具有零值的列

python - 将金额 (int) 转换为 BCD