List of language codes (ISO639-1) in Python?

Can I get a list of all ISO639-1 language codes from picountry 1.15? For example, ['en', 'it', 'el', 'fr', ...]? If so, how?

I'm not afraid:

import pycountry
pycountry.languages
+4
source share
1 answer

This will give you a list of two-digit ISO3166-1 country codes:

countries = [country.alpha2 for country in pycountry.countries]

This will give you a list of two-digit ISO639-1 language codes:

langs = [lang.iso639_1_code
         for lang in pycountry.languages
         if hasattr(lang, 'iso639_1_code')]

This will give you a list of all language codes:

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]
+7
source

Source: https://habr.com/ru/post/1608911/


All Articles