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

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

那么,python 2 pickle不能加载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.

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

If I want it? How to do?


回答 0

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

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

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

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

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.

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

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

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


回答 1

Pickle使用不同的protocols方法将您的数据转换为二进制流。

3为了能够在python 2中加载数据,您必须在python 3中指定一个低于的协议。可以protocol在调用时指定参数pickle.dump

Pickle uses different protocols to convert your data to a binary stream.

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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。