+3 votes
in Programming Languages by (56.7k points)

I am trying to load a pickle file using the following code, but it is giving error:

>>> with open('PS118220_TrainingData.pkl','r') as f:
...     data=pickle.load(f)

The error is:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib64/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

What is wrong with the above code? 

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

You are opening the pickle file in 'r' mode and hence it is giving the error. Use 'rb' mode instead of 'r' mode and you will not get the error.

>>> with open('PS118220_TrainingData.pkl','rb') as f:
...     data=pickle.load(f)


...