dam*_*mon 2 python unicode encoding decode
我知道这bytes.decode给出了一个字符串并string.encode给出了字节,但前提encoding是使用了正确的。
假设我有一个使用编码的字节对象gb18030
如果我尝试使用big5以下方法对其进行解码:
>>name = '?? damon'
>>b1 = name.encode('gb18030')
>>> b1.decode('big5')
UnicodeDecodeError: 'big5' codec can't decode byte 0xc8 in position 2: illegal multibyte sequence
Run Code Online (Sandbox Code Playgroud)
有什么方法可以从bytes对象中找到编码吗?
我在python3文档中找不到这方面的任何有用的 api 。
如果您使用的是 Ubuntu:
sudo apt-get install python3-chardet
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 pip:
pip install chardet2
Run Code Online (Sandbox Code Playgroud)