Adding encoding alias to python

跟風遠走 提交于 2019-12-30 06:59:27

问题


Is there a way that I can add alias to python for encoding. There are sites on the web that are using the encoding 'windows-1251' but have their charset set to win-1251, so I would like to have win-1251 be an alias to windows-1251


回答1:


The encodings module is not well documented so I'd instead use codecs, which is:

import codecs

def encalias(oldname, newname):
  old = codecs.lookup(oldname)
  new = codecs.CodecInfo(old.encode, old.decode, 
                         streamreader=old.streamreader,
                         streamwriter=old.streamwriter,
                         incrementalencoder=old.incrementalencoder,
                         incrementaldecoder=old.incrementaldecoder,
                         name=newname)
  def searcher(aname):
    if aname == newname:
      return new
    else:
      return None
  codecs.register(searcher)

This is Python 2.6 -- the interface is different in earlier versions.

If you don't mind relying on a specific version's undocumented internals, @Lennart's aliasing approach is OK, too, of course - and indeed simpler than this;-). But I suspect (as he appears to) that this one is more maintainable.




回答2:


>>> import encodings
>>> encodings.aliases.aliases['win_1251'] = 'cp1251'
>>> print '\xcc\xce\xd1K\xc2\xc0'.decode('win-1251')
MOCKBA

Although I personally would consider this monkey-patching, and use my own conversion table. But I can't give any good arguments for that position. :)




回答3:


Encoding aliases can be added by editing aliases.py file.

# euc_jp codec
'eucjp'              : 'euc_jp',
'ujis'               : 'euc_jp',
'u_jis'              : 'euc_jp',
'euc_jp_linux'       : 'euc_jp',
'euc-jp-linux'       : 'euc_jp',

Above I have added two aliases euc_jp_linux and euc-jp-linux to the encoding euc_jp.

For a 64 bit linux system aliases.py file is generally located under /usr/lib64/python2.6/encodings/



来源:https://stackoverflow.com/questions/1064086/adding-encoding-alias-to-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!