UnicodeDecodeError: 'ascii' codec cannot decode byte 0x8b
I am using the following code here (with minor modifications):
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 if I use 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 problem?
Thank.
+3
Simplicity
source
to share
2 answers
Since it fails when encoding characters
Try using Latin
cPickle.load(file, encoding='latin1')
+8
Robert I
source
to share
replace:
dict = cPickle.load(fo)
in unpickle function with:
dict = cPickle.load(fo, encoding='latin1')
+1
ChaosPredictor
source
to share