2012-06-17 54 views
6

我正在尝试遵循本书“挖掘社交网络”1-3的示例代码。twitter趋势api UnicodeDecodeError:'utf8'编解码器无法解码位置1中的字节0x8b:意外的代码字节

[ trend.decode('utf-8') for trend in world_trends()[0]['trends'] ] 

和错误信息:

我知道它的老,所以我遵循网页enter link description here

,而且有时候新的样本,我会在我执行的代码遭受错误信息是这样的:

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "build/bdist.macosx-10.6-universal/egg/twitter/api.py", line 167, in __call__ 
File "build/bdist.macosx-10.6-universal/egg/twitter/api.py", line 173, in _handle_response 
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/utf_8.py", line 16, in decode 
return codecs.utf_8_decode(input, errors, True) 
UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: unexpected code byte 

它并不总是会发生,但我认为没有程序员喜欢这样的“随机”情况。

所以有谁能帮我解决这个问题?什么是问题以及我如何解决这个问题?

十分感谢〜

+0

我使用python请求见状在库中。 '0x8b'来自gzip头:'1F 8B 08'。有些东西缺少这是gzip压缩数据的事实。 –

+1

更新:这是由于[请求的错误2561](https://github.com/kennethreitz/requests/issues/2561)。 –

回答

1

默认解码(),如果它遇到一个字节,它不知道如何解码将抛出一个错误。

您可以使用trend.decode('utf-8', 'replace')trend.decode('utf-8', 'ignore')不会抛出错误并默默忽略它。

Documentation on decode() here.

16

byte 0x8b in position 1通常标志着该数据流被gzip压缩。有关类似问题,请参见herehere

要解压缩数据流:

buf = StringIO.StringIO(<response object>.content) 
gzip_f = gzip.GzipFile(fileobj=buf) 
content = gzip_f.read() 
+0

谢谢你指出这一点。帮助我发现我不经意地压缩了一些本不应该被压缩的文件。 – dmh

相关问题