ValueError:不支持的泡菜协议:3,python2泡菜无法加载python 3泡菜转储的文件? [英] ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
问题描述
我使用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 protocol
parameter 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 protocol
parameter in pickle.load
because pickle
can determine the protocol from the file.
这篇关于ValueError:不支持的泡菜协议:3,python2泡菜无法加载python 3泡菜转储的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!