[英]ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
I use pickle to dump a file on python 3, and I use pickle to load the file on python 2, the ValueError appears. 我使用pickle在python 3上转储文件,我使用pickle在python 2上加载文件,出现ValueError。
So, python 2 pickle can not load the file dumped by python 3 pickle? 那么,python 2 pickle无法加载python 3 pickle转储的文件?
If I want it? 如果我想要它? How to do?
怎么做?
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. 您应该在Python 3中使用较低的协议编号编写pickle数据.Python 3引入了一个编号为
3
的新协议(并将其用作默认值),因此切换回值2
,可由Python 2读取。
Check the protocol
parameter in pickle.dump
. 检查
pickle.dump
的protocol
参数。 Your resulting code will look like this. 生成的代码将如下所示。
pickle.dump(your_object, your_file, protocol=2)
There is no protocol
parameter in pickle.load
because pickle
can determine the protocol from the file. pickle.load
没有protocol
参数,因为pickle
可以从文件中确定协议。
Pickle uses different protocols
to convert your data to a binary stream. Pickle使用不同的
protocols
将您的数据转换为二进制流。
In python 2 there are 3 different protocols ( 0
, 1
, 2
) and the default is 0
. 在Python 2有3个不同的协议 (
0
, 1
, 2
),默认是0
。
In python 3 there are 5 different protocols ( 0
, 1
, 2
, 3
, 4
) and the default is 3
. 在Python 3有5层不同的协议 (
0
, 1
, 2
, 3
, 4
),默认是3
。
You must specify in python 3 a protocol lower than 3
in order to be able to load the data in python 2. You can specify the protocol
parameter when invoking pickle.dump
. 必须在Python 3的协议低于指定
3
,以便能够加载在python 2.数据可以指定protocol
调用时的参数pickle.dump
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.