简体   繁体   English

ValueError:不支持的pickle协议:3,python2 pickle无法加载python 3 pickle转储的文件?

[英]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.dumpprotocol参数。 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将您的数据转换为二进制流。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM