答案 0 :(得分:3)
我假设你想要ISO 639 2而不是ISO 639 3。机器可读数据可从Library of Congress获得(我对此答案使用“utf-8”编码,有关详细信息,另请参阅http://www.loc.gov/standards/iso639-2/ascii_8bits.html。)
以下是如何加载此内容的示例:
import codecs
def getisocodes_dict(data_path):
# Provide a map from ISO code (both bibliographic and terminologic)
# in ISO 639-2 to a dict with the two letter ISO 639-2 codes (alpha2)
# English and french names
#
# "bibliographic" iso codes are derived from English word for the language
# "terminologic" iso codes are derived from the pronunciation in the target
# language (if different to the bibliographic code)
D = {}
f = codecs.open(data_path, 'rb', 'utf-8')
for line in f:
iD = {}
iD['bibliographic'], iD['terminologic'], iD['alpha2'], \
iD['english'], iD['french'] = line.strip().split('|')
D[iD['bibliographic']] = iD
if iD['terminologic']:
D[iD['terminologic']] = iD
if iD['alpha2']:
D[iD['alpha2']] = iD
for k in iD:
# Assign `None` when columns not available from the data
iD[k] = iD[k] or None
f.close()
return D
if __name__ == '__main__':
D = getisocodes_dict('ISO-639-2_utf-8.txt')
print D['eng']
print D['fr']
# Print my current locale
import locale
print D[locale.getdefaultlocale()[0].split('_')[0].lower()]
答案 1 :(得分:3)
您还可以在http://pypi.python.org/pypi/pycountry/使用pycountry
,{{3}}似乎有ISO 639 2代码(仅使用谷歌: - )
答案 2 :(得分:-1)
您可以使用getdefaultlocale
function in the locale
module。它返回元组中系统默认语言环境的语言代码和编码。然后你可以使用一小段额外的代码来获得ISO 639 2代码:
>>> import locale
>>> lang = list(locale.getdefaultlocale())
['en_GB', 'cp1252']
>>> lang = lang[1][0:2]
'en'