ValueError: unsupported pickle protocol: 3, python2 pickle无法加载python 3 pickle转储的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25843698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:44:06  来源:igfitidea点击:

ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?

pythonpython-3.xpython-2.7picklevalueerror

提问by Aleeee

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?

如果我想要?怎么做?

采纳答案by Matthias

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 2which can be read by Python 2.

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

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

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

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

There is no protocolparameter in pickle.loadbecause picklecan determine the protocol from the file.

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

回答by enrico.bacis

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

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

You must specify in python 3 a protocol lower than 3in order to be able to load the data in python 2. You can specify the protocolparameter when invoking pickle.dump.

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