unpickle OrderedDict from python3 in python2

我们两清 提交于 2019-12-06 05:50:21

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