python2与python3保存的pickle文件不兼容问题

1.python3 读取 python2保存的pickle文件

import pickle
inf=pickle.load(open('f:\\a.pkl',"rb"),encoding='latin1')
print(inf)
f.close()

2.python2 读取 python3保存的pickle文件

报错为:

ValueError: unsupported pickle protocol: 4

原因:python2 不支持读取 protocol>2

1)在python3中将数据格式转换成python2可读的数据格式:
 

with open('../data.pkl', 'rb') as f:
    w = pickle.load(f)
pickle.dump(w, open('../data1.pkl',"wb"), protocol=2)

2)即可在python2环境内操作

猜你喜欢

转载自blog.csdn.net/qq_33373858/article/details/83862381