Fixed python3 reading pickle files stored in Python2

  • 2021-01-03 20:56:48
  • OfStack

I am using python3.5 to handle a serialized file xxx.pk, but this.pk file I have stored in python2.7 will report the following error when I read it using python3.


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

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

Solutions:


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

Related articles: