7

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.

2 Answers 2

17

Since it fails on the encoding of the characters

Try using latin

cPickle.load(file, encoding='latin1')
Sign up to request clarification or add additional context in comments.

Comments

1

replace:

dict = cPickle.load(fo)

in unpickle function with:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.