2

我想要一个可以执行以下操作的函数:

def get_lang(enc):
  ...

>>> get_lang('ascii')
'English'
>>> get_lang('big5')
'Traditional Chinese'
>>> get_lang('utf-8')
'All languages'

是否可以?

更新:我的意思是,就像手册中这样:http: //docs.python.org/library/codecs.html#standard-encodings

4

2 回答 2

1

是的,你可以使用这本字典:

codec_langs = {'ascii':'English',
'big5': 'Traditional Chinese',
'big5hkscs':'Traditional Chinese',
'cp037':'English',
'cp424':'Hebrew',
'cp437':'English',
'cp500':'Western Europe',
'cp720':'Arabic',
'cp737':'Greek',
'cp775':'Baltic languages',
'cp850':'Western Europe',
'cp852':'Central and Eastern Europe',
'cp855':'Bulgarian, Byelorussian, Macedonian, Russian, Serbian',
'cp856':'Hebrew',
'cp857':'Turkish',
'cp858':'Western Europe',
'cp860':'Portuguese',
'cp861':'Icelandic',
'cp862':'Hebrew',
'cp863':'Canadian',
'cp864':'Arabic',
'cp865':'Danish, Norwegian',
'cp866':'Russian',
'cp869':'Greek',
'cp874':'Thai',
'cp875':'Greek',
'cp932':'Japanese',
'cp949':'Korean',
'cp950':'Traditional Chinese',
'cp1006':'Urdu',
'cp1026':'Turkish',
'cp1140':'Western Europe',
'cp1250':'Central and Eastern Europe',
'cp1251':'Bulgarian, Byelorussian, Macedonian, Russian, Serbian',
'cp1252':'Western Europe',
'cp1253':'Greek',
'cp1254':'Turkish',
'cp1255':'Hebrew',
'cp1256':'Arabic',
'cp1257':'Baltic languages',
'cp1258':'Vietnamese',
'euc_jp':'Japanese',
'euc_jis_2004':'Japanese',
'euc_jisx0213':'Japanese',
'euc_kr':'Korean',
'gb2312':'Simplified Chinese',
'gbk 936':'Unified Chinese',
'gb18030':'Unified Chinese',
'hz  hzgb':'Simplified Chinese',
'iso2022_jp':'Japanese',
'iso2022_jp_1':'Japanese',
'iso2022_jp_2':'Japanese, Korean, Simplified ,Chinese, Western Europe, Greek',
'iso2022_jp_2004':'Japanese',
'iso2022_jp_3':'Japanese',
'iso2022_jp_ext':'Japanese',
'iso2022_kr':'Korean',
'latin_1':'West Europe',
'iso8859_2':'Central and Eastern Europe',
'iso8859_3':'Esperanto, Maltese',
'iso8859_4':'Baltic languages',
'iso8859_5':'Bulgarian, Byelorussian, Macedonian, ,Russian, Serbian',
'iso8859_6':'Arabic',
'iso8859_7':'Greek',
'iso8859_8':'Hebrew',
'iso8859_9':'Turkish',
'iso8859_10':'Nordic languages',
'iso8859_13':'Baltic languages',
'iso8859_14':'Celtic languages',
'iso8859_15':'Western Europe',
'iso8859_16':'South-Eastern Europe',
'johab':'Korean',
'koi8_r':'Russian',
'koi8_u':'Ukrainian',
'mac_cyrillic':'Bulgarian, Byelorussian, Macedonian, Russian, ,Serbian',
'mac_greek':'Greek',
'mac_iceland':'Icelandic',
'mac_latin2':'Central and Eastern Europe',
'mac_roman':'Western Europe',
'mac_turkish':'Turkish',
'ptcp154':'Kazakh',
'shift_jis':'Japanese',
'shift_jis_2004':'Japanese',
'shift_jisx0213':'Japanese',
'utf_32':'all languages',
'utf_32_be':'all languages',
'utf_32_le':'all languages',
'utf_16':'all languages',
'utf_16_be':'all languages (BMP only)',
'utf_16_le':'all languages (BMP only)',
'utf_7':'all languages',
'utf_8':'all languages',
'utf_8_sig':'all languages'}
于 2012-07-04T09:02:45.243 回答
1

不,没有任何广泛可用的模块包含该信息。

我建议解析文档(来自http://hg.python.org/cpython/file/tip/Doc/library/codecs.rst#l926http://docs.python.org/_sources/library/codecs .txt)来构造一个查找字典,就像@fraxel 似乎所做的那样。由于编解码器很少被添加到 Python 中(并且越来越多地被 Unicode 取代),这种方法应该是合理的面向未来的。

于 2012-07-04T09:11:41.340 回答