<i id='JR1ML'><tr id='JR1ML'><dt id='JR1ML'><q id='JR1ML'><span id='JR1ML'><b id='JR1ML'><form id='JR1ML'><ins id='JR1ML'></ins><ul id='JR1ML'></ul><sub id='JR1ML'></sub></form><legend id='JR1ML'></legend><bdo id='JR1ML'><pre id='JR1ML'><center id='JR1ML'></center></pre></bdo></b><th id='JR1ML'></th></span></q></dt></tr></i><div id='JR1ML'><tfoot id='JR1ML'></tfoot><dl id='JR1ML'><fieldset id='JR1ML'></fieldset></dl></div>

      <tfoot id='JR1ML'></tfoot>
      • <bdo id='JR1ML'></bdo><ul id='JR1ML'></ul>

      <small id='JR1ML'></small><noframes id='JR1ML'>

      <legend id='JR1ML'><style id='JR1ML'><dir id='JR1ML'><q id='JR1ML'></q></dir></style></legend>
      1. 用 python 3 解开一个 python 2 对象

        Unpickling a python 2 object with python 3(用 python 3 解开一个 python 2 对象)

            <tbody id='eTVS0'></tbody>
          <tfoot id='eTVS0'></tfoot>
          <i id='eTVS0'><tr id='eTVS0'><dt id='eTVS0'><q id='eTVS0'><span id='eTVS0'><b id='eTVS0'><form id='eTVS0'><ins id='eTVS0'></ins><ul id='eTVS0'></ul><sub id='eTVS0'></sub></form><legend id='eTVS0'></legend><bdo id='eTVS0'><pre id='eTVS0'><center id='eTVS0'></center></pre></bdo></b><th id='eTVS0'></th></span></q></dt></tr></i><div id='eTVS0'><tfoot id='eTVS0'></tfoot><dl id='eTVS0'><fieldset id='eTVS0'></fieldset></dl></div>

          <small id='eTVS0'></small><noframes id='eTVS0'>

            <bdo id='eTVS0'></bdo><ul id='eTVS0'></ul>
                <legend id='eTVS0'><style id='eTVS0'><dir id='eTVS0'><q id='eTVS0'></q></dir></style></legend>

                  本文介绍了用 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_importsencodingerrors,用于控制对Python 2生成的pickle流的兼容性支持.如果fix_imports 为真,pickle 将尝试将旧的 Python 2 名称映射到 Python 3 中使用的新名称.encodingerrors 告诉 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'.

                  这篇关于用 python 3 解开一个 python 2 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                  <legend id='Htqwb'><style id='Htqwb'><dir id='Htqwb'><q id='Htqwb'></q></dir></style></legend>

                    <bdo id='Htqwb'></bdo><ul id='Htqwb'></ul>
                      <tbody id='Htqwb'></tbody>
                  • <i id='Htqwb'><tr id='Htqwb'><dt id='Htqwb'><q id='Htqwb'><span id='Htqwb'><b id='Htqwb'><form id='Htqwb'><ins id='Htqwb'></ins><ul id='Htqwb'></ul><sub id='Htqwb'></sub></form><legend id='Htqwb'></legend><bdo id='Htqwb'><pre id='Htqwb'><center id='Htqwb'></center></pre></bdo></b><th id='Htqwb'></th></span></q></dt></tr></i><div id='Htqwb'><tfoot id='Htqwb'></tfoot><dl id='Htqwb'><fieldset id='Htqwb'></fieldset></dl></div>

                          <tfoot id='Htqwb'></tfoot>

                          1. <small id='Htqwb'></small><noframes id='Htqwb'>