unpickle OrderedDict from python3 in python2

Deadly 提交于 2019-12-22 23:26:29

问题


I'm trying to unpickle objects pickled in python3. This works in python3 but not in python2. The issue can be reproduced down to pickle protocol 0. Example code:

import pickle
import collections

o = collections.OrderedDict([(1,1),(2,2),(3,3),(4,4)])
f = open("test.pkl", "wb")
pickle.dump(o, f, 0)
f.close()

This results in the following pkl file:

python2:

ccollections
OrderedDict
p0
((lp1
(lp2
I1
aI1
aa(lp3
I2
aI2
aa(lp4
I3
aI3
aa(lp5
I4
aI4
aatp6
Rp7

python3:

cUserString
OrderedDict
p0
(tRp1
L1L
L1L
sL2L
L2L
sL3L
L3L
sL4L
L4L
s.

When I try to load the pickle file created in python3 from python2 I'm getting the following exception:

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> f = open("test.pkl", "rb")
>>> p = pickle.load(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 1090, in load_global
    klass = self.find_class(module, name)
  File "/usr/lib/python2.7/pickle.py", line 1126, in find_class
    klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'OrderedDict

However changing first line in the pickle file from UserString to collections sort of solves the problem.

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> f = open("test.pkl", "rb")
>>> p = pickle.load(f)
>>> p
OrderedDict([(1L, 1L), (2L, 2L), (3L, 3L), (4L, 4L)])

Is this a bug in pickle in python3?


回答1:


Make sure you import collections in Python 2. This code works for me:

Python 3 - do the pickling:

import pickle
import collections

o = collections.OrderedDict([(1,1),(2,2),(3,3),(4,4)])
with open('/home/bo/Desktop/test.pkl', 'wb') as f:
    pickle.dump(o, f, 2)

Python 2 - do the unpickling:

import pickle
import collections

with open('/home/bo/Desktop/test.pkl', 'rb') as f:
    o = pickle.load(f)

When I do that I can read o with no issue:

>>> o
0: OrderedDict([(1, 1), (2, 2), (3, 3), (4, 4)])


来源:https://stackoverflow.com/questions/33568844/unpickle-ordereddict-from-python3-in-python2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!