发布于 2010-05-21 14:22:38
我猜你想要的是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()]
发布于 2010-05-21 14:36:11
你也可以在http://pypi.python.org/pypi/pycountry/上使用pycountry
,它似乎有ISO639 2代码(只需使用谷歌:-)
https://stackoverflow.com/questions/2879856
复制相似问题