ValueError:不支持的泡菜协议:3,python2泡菜无法加载python 3泡菜转储的文件? [英] ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?

查看:114
本文介绍了ValueError:不支持的泡菜协议:3,python2泡菜无法加载python 3泡菜转储的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用pickle在python 3上转储文件,并使用pickle在python 2上加载文件,出现ValueError.

I use pickle to dump a file on python 3, and I use pickle to load the file on python 2, the ValueError appears.

那么,python 2 pickle无法加载python 3 pickle转储的文件吗?

So, python 2 pickle can not load the file dumped by python 3 pickle?

是否要?怎么办?

推荐答案

您应该在Python 3中以较低的协议编号编写腌制的数据.Python3引入了编号为3的新协议(并将其用作默认协议) ),因此请切换回2的值,该值可以由Python 2读取.

You should write the pickled data with a lower protocol number in Python 3. Python 3 introduced a new protocol with the number 3 (and uses it as default), so switch back to a value of 2 which can be read by Python 2.

pickle.dump 中检查protocol参数.您生成的代码将如下所示.

Check the protocolparameter in pickle.dump. Your resulting code will look like this.

pickle.dump(your_object, your_file, protocol=2)

pickle.load 中没有protocol参数,因为pickle可以从文件中确定协议.

There is no protocolparameter in pickle.load because pickle can determine the protocol from the file.

这篇关于ValueError:不支持的泡菜协议:3,python2泡菜无法加载python 3泡菜转储的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆