使用 python 3 解压 python 2 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28218466/
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-19 02:56:02  来源:igfitidea点击:

Unpickling a python 2 object with python 3

pythonpython-3.xpicklepython-2.4python-2to3

提问by NDevox

I'm wondering if there is a way to load an object that was pickled in Python 2.4, with Python 3.4.

我想知道是否有办法使用 Python 3.4 加载在 Python 2.4 中腌制的对象。

I've been running 2to3 on a large amount of company legacy code to get it up to date.

我一直在大量公司遗留代码上运行 2to3 以使其保持最新状态。

Having done this, when running the file I get the following error:

完成此操作后,运行文件时出现以下错误:

  File "H:\fixers - 3.4\addressfixer - 3.4\trunk\lib\address\address_generic.py"
, line 382, in read_ref_files
    d = pickle.load(open(mshelffile, 'rb'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1: ordinal
not in range(128)

Looking at the pickled object in contention, it's a dictin a dict, containing keys and values of type str.

查看争用的腌制对象,它是 a dictin a dict,包含 type 的键和值str

So my question is: Is there a way to load an object, originally pickled in python 2.4, with python 3.4?

所以我的问题是:有没有办法用python 3.4加载最初在python 2.4中腌制的对象?

采纳答案by Martijn Pieters

You'll have to tell pickle.load()how to convert Python bytestring data to Python 3 strings, or you can tell pickleto leave them as bytes.

您必须告诉pickle.load()如何将 Python 字节串数据转换为 Python 3 字符串,或者您可以告诉pickle将它们保留为字节。

The default is to try and decode all string data as ASCII, and that decoding fails. See the pickle.load()documentation:

默认是尝试将所有字符串数据解码为 ASCII,但解码失败。请参阅pickle.load()文档

Optional keyword arguments are fix_imports, encodingand errors, which are used to control compatibility support for pickle stream generated by Python 2. If fix_importsis true, pickle will try to map the old Python 2 names to the new names used in Python 3. The encodingand errorstell pickle how to decode 8-bit string instances pickled by Python 2; these default to ‘ASCII' and ‘strict', respectively. The encodingcan be ‘bytes' to read these 8-bit string instances as bytes objects.

可选的关键字参数是fix_importsencodingerrors,用于控制对 Python 2 生成的 pickle 流的兼容性支持。如果fix_imports为 true,pickle 将尝试将旧的 Python 2 名称映射到 Python 3 中使用的新名称。encodingerrors告诉 pickle 如何解码 Python 2 腌制的 8 位字符串实例;这些分别默认为 'ASCII' 和 'strict'。该编码可以是“字节”来读取这些8位串实例作为字节对象。

Setting the encoding to latin1allows you to import the data directly:

将编码设置为latin1允许您直接导入数据:

with open(mshelffile, 'rb') as f:
    d = pickle.load(f, encoding='latin1') 

but you'll need to verify that none of your strings are decoded using the wrong codec; Latin-1 works for any input as it maps the byte values 0-255 to the first 256 Unicode codepoints directly.

但是您需要验证您的任何字符串都没有使用错误的编解码器进行解码;Latin-1 适用于任何输入,因为它将字节值 0-255 直接映射到前 256 个 Unicode 代码点。

The alternative would be to load the data with encoding='bytes', and decode all byteskeys and values afterwards.

另一种方法是使用 加载数据encoding='bytes',然后解码所有bytes键和值。

Note that up to Python versions before 3.6.8, 3.7.2 and 3.8.0, unpickling of Python 2 datetimeobject data is brokenunless you use encoding='bytes'.

需要注意的是高达Python版本之前3.6.8,3.7.2和3.8.0,取储存的Python 2的datetime对象数据被破坏,除非你使用encoding='bytes'

回答by Sreeragh A R

Using encoding='latin1'causes some issues when your object contains numpy arrays in it.

encoding='latin1'当您的对象包含 numpy 数组时,使用会导致一些问题。

Using encoding='bytes'will be better.

使用encoding='bytes'会更好。

Please see this answerfor complete explanation of using encoding='bytes'

有关使用的完整说明,请参阅此答案encoding='bytes'