我在Python3.2中使用chardet2.01,源码就像这个站点的http://getpython3.com/diveintopython3/case-study-porting-chardet-to-python-3.html
可在此处下载
http://jaist.dl.sourceforge.net/project/cygwin-ports/release-2/Python/python3-chardet/python3-chardet-2.0.1-2.tar.bz2
我使用lxml2解析html以获得一些字符串
,并使用以下代码来检测编码
chardet.detect(name)但是发生了错误
Traceback (most recent call last):
File "C:\python\test.py", line 125, in <module>
print(chardet.detect(str(name)))
File "E:\Python32\lib\site-packages\chardet\__init__.py", line 24, in detect
u.feed(aBuf)
File "E:\Python32\lib\site-packages\chardet\universaldetector.py", line 98, in feed
if self._highBitDetector.search(aBuf):
TypeError: can't use a bytes pattern on a string-like objectname是一个字符串对象
将字符串转换为字节意味着使用诸如'utf-8','big5‘这样的编码对其进行编码
以此类推,charset将检测您made....not原始字符串的编码
我对这个问题一无所知...
发布于 2012-09-10 23:11:49
问题很明显,您正在对字符串而不是字节对象调用chardet。您缺少的是,对于Python,字符串已经解码为。它不再有编码。
您必须修复您的代码,以便它在将原始字节解码为字符串之前为chardet提供这些字节。如果您从另一个包中获取字符串,那么它已经确定了编码,您无能为力。
https://stackoverflow.com/questions/12353682
复制相似问题