python2 python3使用pickle不兼容

来源:互联网 发布:php开发微信公众平台 编辑:程序博客网 时间:2024/05/23 19:42

阿里的pai平台是 python2.x,在python3下的代码经常会有问题。

比如python3的str可以用来编码,python的str只是转为字符串,需要用str(1).encode('utf-8').decode('utf-8')这样。

在python3中序列化Python2不能识别,这时只要这样序列化:

python3:

with open(pwd,'wb') as f:

pickle.dump(x,f,protocol = 2)

这样序列化后的x在Python2里面就能读取

python2:

with open(pwd,'rb') as f:

pickle.dump(f)


阅读全文
1 0
原创粉丝点击