SoFunction
Updated on 2024-11-13

Troubleshooting python3 reading pickle files stored by python2

I'm using python 3.5 to process a serialized file, however the .pk file is one that I have stored inside python 2.7, and when I read it with python 3 it reports the following error.

import pickle
picklefile=open('2ohsumed_wmd_d.pk','rb')
data=(picklefile)
 
print (data)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 11: ordinal not in range(128)

Solution:

import pickle
picklefile=open('2ohsumed_wmd_d.pk','rb')
data=(picklefile,encoding='iso-8859-1')
 
print (data)

The above article to solve the python3 read Python2 stored pickle file problem is all I have shared with you, I hope to give you a reference, and I hope you support me more.