在python2中加载pickle文件(来自python3)

时间:2015-04-12 07:42:24

标签: python-2.7 python-3.x pickle

我有一个pickle文件,

>>> with open("wikilinks.pickle", "rb") as f:
...     titles, links = pickle.load(f)
... 
>>> len(titles)
13421

我可以在python3中加载它。 但是,当我尝试在python2中加载它时,我得到以下消息: 回溯(最近一次调用最后一次):

  File "<stdin>", line 2, 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 886, in load_proto
    raise ValueError, "unsupported pickle protocol: %d" % proto
ValueError: unsupported pickle protocol: 3

那么如何在python2中加载它?

2 个答案:

答案 0 :(得分:25)

读入python3并将其保存为python2格式

#!/usr/bin/env python3

import pickle

with open("a.pkl", "rb") as f:
    w = pickle.load(f)

pickle.dump(w, open("a_py2.pkl","wb"), protocol=2)

然后你可以在python2中加载a_py2.pkl

答案 1 :(得分:14)

Python 3中默认使用的default pickling protocol与Python 2使用的协议不兼容。但是,如果您可以修改生成wikilink.pickle的代码,您可以告诉它使用其中一个早期的协议(0,1或2),Python 2将理解这一点。

但请注意,Python 3字符串将在Python 2中作为Unicode字符串出现,在某些用例中可能会令人惊讶。

$ python3
Python 3.4.1 (default, May 19 2014, 17:23:49) 
[GCC 4.9.0 20140507 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> pickle.dumps([1, 2, 'abc'], 2)
b'\x80\x02]q\x00(K\x01K\x02X\x03\x00\x00\x00abcq\x01e.'
>>> 
$ python 
Python 2.7.8 (default, Jul  1 2014, 17:30:21) 
[GCC 4.9.0 20140604 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cPickle
>>> cPickle.loads('\x80\x02]q\x00(K\x01K\x02X\x03\x00\x00\x00abcq\x01e.')
[1, 2, u'abc']