之前一直想找到一个模块,针对字符判断是什么字符集编码的库
网上有chardet的blog,发现自己的环境有这个库,于是就做了测试
>>> import chardet >>> a = "也有".decode('gbk') >>> a u'u6d94u71b8u6e41' >>> a = "也有".decode('gbk').encode('gbk') >>> a 'xe4xb9x9fxe6x9cx89' >>> chardet.detect(a) {'confidence': 0.7525, 'language': '', 'encoding': 'utf-8'} >>> print(a) 也有>>> a = "也有".decode('gbk').encode('gbk') >>> a = u"也有".encode('gbk') >>> a 'xd2xb2xd3xd0' >>> chardet.detect(a) {'confidence': 0.7679697235616183, 'language': 'Russian', 'encoding': 'KOI8-R'}
In [5]: a = "asd算法".encode('utf-8').decode('gbk')
In [6]: import chardet
In [7]: chardet.detect(a)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-45a625c5d441> in <module>()
----> 1 chardet.detect(a)
c:python3.6.3libsite-packageschardet\__init__.py in detect(byte_str)
32 if not isinstance(byte_str, bytes):
33 raise TypeError('Expected object of type bytes or bytearray, got: '
---> 34 '{0}'.format(type(byte_str)))
35 else:
36 byte_str = bytearray(byte_str)
TypeError: Expected object of type bytes or bytearray, got: <class 'str'>
In [8]: a
Out[8]: 'asd绠楁硶'
总结发现不是很准,放弃使用