1

I've tried to read and load pickle files in Python3.

import pickle as pickle


pickleFileName = 'data/fingerDataSet' + '.pickle'
pickleFile = open(pickleFileName, 'rb')

data = pickle.load(pickleFile)
pickleFile.close()

but in line data = pickle.load(pickleFile) I'm getting strange error UnicodeDecodeError: 'ascii' codec can't decode byte 0xc0 in position 5: ordinal not in range(128)

Jakub Swistak
  • 304
  • 3
  • 10
  • How was the file written and what did it contain? – Axe319 Jan 10 '20 at 18:54
  • In unicode, `0xc0` is the letter À. So the problem should be in the file – user8408080 Jan 10 '20 at 18:55
  • I meant more along the lines of what python object was pickled and written to the file? – Axe319 Jan 10 '20 at 18:56
  • I've found some hosting and uploaded file there so https://uploadfiles.io/90rlmu5d It should be an image of a finger, this works in Python2 so the file is ok. – Jakub Swistak Jan 10 '20 at 19:04
  • Unpickling from an untrusted source is a bad idea. Additionally, unpickling data with python 3 that was pickled in python 2 has it's own issues. https://stackoverflow.com/questions/28218466/unpickling-a-python-2-object-with-python-3 – Axe319 Jan 10 '20 at 19:16

1 Answers1

1

Pickle is not intended to work across different Python versions. What is likely in this case is that the original data was in a Python 2 str, which contains bytes, and the Python3 version is trying to read it as text, performing an implicit decode.

What you should do there is to unpickle your data in a Python 2 environment, and save it in a way that is not dependente on the Python version (if it is an image, use the PIL library to write a PNG file, for example)

jsbueno
  • 99,910
  • 10
  • 151
  • 209