[英]How to load a Python 3 pickle in Python 2.7 *Without changing it*?
I am migrating a large piece of software from python 2.7 to python 3.7我正在将一大块软件从 python 2.7 迁移到 python 3.7
For back-testing purposes, I need to be able to read old (2.7) pickles in new (3.7) python, and vice versa.出于回溯测试的目的,我需要能够在新的(3.7)python 中读取旧的(2.7)泡菜,反之亦然。
I can read old pickles in new python. 我可以在新 python 中读取旧泡菜。
I want to also be able to read new pickles in old python.我还希望能够在旧 python 中读取新泡菜。
I found this answer , but it is not good for me, as it requires me to change the pickle. 我找到了这个答案,但它对我不利,因为它需要我更换泡菜。
Is there a way to read a python 3 pickle in python 2 without changing the pickle ?有没有办法在不改变泡菜的情况下在 python 2 中读取 python 3泡菜?
This is the best solution I could find:这是我能找到的最佳解决方案:
Since I am able to change the new software, but not the old one, every time I create a pickle which has to be read by the old software, I do由于我可以更改新软件,但不能更改旧软件,因此每次创建必须由旧软件读取的泡菜时,我都会这样做
def create_old_pickle(new_pickle):
loaded = pickle.loads(new_pickle)
repickled = pickle.dumps(loaded, protocol=2)
return repickled
This creates old pickles in new python.这会在新 python 中创建旧泡菜。
Thus old software API remains the same.因此旧的软件 API 保持不变。
You may notice unicode->bytes problems, but that's something else.您可能会注意到 unicode->bytes 问题,但那是另外一回事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.