Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 679
Simplicity Avatar asked Mar 22 '17 00:03

Simplicity


2 Answers

Since it fails on the encoding of the characters

Try using latin

cPickle.load(file, encoding='latin1')
like image 184
Robert I Avatar answered Oct 20 '22 08:10

Robert I


replace:

dict = cPickle.load(fo)

in unpickle function with:

dict = cPickle.load(fo, encoding='latin1')
like image 29
ChaosPredictor Avatar answered Oct 20 '22 08:10

ChaosPredictor