UnicodeDecodeError:'ascii'编解码器无法解码字节0x8b

时间:2017-03-22 00:54:28

标签: python unicode codec

我正在使用here中的以下代码(进行少量修改):

import _pickle as cPickle 

def unpickle(file):
    fo = open(file, 'rb')
    dict = cPickle.load(fo)
    fo.close()
    return dict

unpickle('data_batch_1')

当我运行代码时,如果我使用Python 3.5.2,我会得到以下内容:

Traceback (most recent call last):
  File "open_batch.py", line 10, in <module>
    unpickle('data_batch_1')
  File "open_batch.py", line 5, in unpickle
    dict = cPickle.load(fo)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6: ordinal not in range(128)

如何解决此问题?

感谢。

2 个答案:

答案 0 :(得分:14)

由于字符编码失败

尝试使用拉丁语

cPickle.load(file, encoding='latin1')

答案 1 :(得分:1)

取代:

dict = cPickle.load(fo)
unpickle 函数中的

dict = cPickle.load(fo, encoding='latin1')