Error when unpickling a file in Python 3.6

Viewed 371

I am quite new to Python and especially to pickling. For an interview data challenge, I was sent a dataset in pickled format but I am having problems unpickling it. They told me the file was pickled on a Mac in Python 3.7. I am using python 3.6.5.

I am using the following code:

import pickle

infile = open('test_data.pickle', 'rb')

pickle.load(infile)

but am then getting the error:

"Invalid load key \xef"

I have read online and tried things such as pickle.load(infile, encoding='bytes') or the same but with 'latin1' instead of bytes. I haven't found any solutions to my problem online or in any other stack exchange questions, so any help would be much appreciated!

Thank you!

Isabel

1 Answers

I had the same error as you described. I removed this file and created it again and it solved the problem.

import pickle

outfile = open('test_data.pickle', 'wb')
pickle.dump({}, outfile)
outfile.close()
infile = open('test_data.pickle', 'rb')
print(pickle.load(infile))
Related