用 python 3 解开一个 python 2 对象

问题描述

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

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

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

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

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

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

  File "H:fixers - 3.4addressfixer - 3.4	runklibaddressaddress_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)

查看竞争中的腌制对象,它是 dict 中的 dict,包含 str 类型的键和值.

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

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

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


解决方案

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

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

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

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

可选的关键字参数是fix_imports、encoding和errors,用于控制对Python 2生成的pickle流的兼容性支持.如果fix_imports 为真,pickle 将尝试将旧的 Python 2 名称映射到 Python 3 中使用的新名称.encoding 和 errors 告诉 pickle如何解码由 Python 2 腌制的 8 位字符串实例;这些默认分别为ASCII"和严格".encoding 可以是bytes",将这些 8 位字符串实例读取为字节对象.

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

将编码设置为latin1可以直接导入数据:

Setting the encoding to latin1 allows you to import the data directly:

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

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

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.

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

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

请注意,在 3.6.8、3.7.2 和 3.8.0 之前的 Python 版本中,对 Python 2 的取消处理 <除非您使用 encoding='bytes',否则 code>datetime 对象数据已损坏.

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

相关文章