UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b

Simplicity picture Simplicity · Mar 22, 2017 · Viewed 9.2k times · Source

I'm using the following code from here (with minor edits):

import _pickle as cPickle 

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

unpickle('data_batch_1')

When I run the code, I get the following, provided that I'm using 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)

How can I fix this issue?

Thanks.

Answer

Robert I picture Robert I · Mar 22, 2017

Since it fails on the encoding of the characters

Try using latin

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