Python2 和 Python3 版本读取 pickle 文件不兼容,需要进行转换处理.

在读取一个 pickle 文件时, 由于刚开始未知其是有 python2 还是 python3 的 pickle 库保存的, 在 python2 环境读取时, 导致出现错误:

import pickle
testpkl = pickle.loads(open("test.pickle", "rb").read())
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/pickle.py", line 1382, in loads return Unpickler(file).load() File "/usr/lib/python2.7/pickle.py", line 858, in load dispatchkey File "/usr/lib/python2.7/pickle.py", line 886, in load_proto raise ValueError, "unsupported pickle protocol: %d" % proto ValueError: unsupported pickle protocol: 3

其原因是, 该test.pickle是 python3 环境下生成的, 因此 python2 读取时不兼容.

解决方案:

#!/usr/bin/env python3
import pickle

import pickle
testpkl = pickle.loads(open("test.pickle", "rb").read())

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

在 python2 环境读取 testpkl_py2.pkl 即可.

Last modification:October 9th, 2018 at 09:31 am