Python泡菜不是一对一的:不同的泡菜给同一个对象 [英] Python pickle not one-to-one: different pickles give same object

查看:81
本文介绍了Python泡菜不是一对一的:不同的泡菜给同一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释吗?

pickle.loads(b'\x80\x03X\x01\x00\x00\x00.q\x00h\x00\x86q\x01.') == pickle.loads(b'\x80\x03X\x01\x00\x00\x00.q\x00X\x01\x00\x00\x00.q\x01\x86q\x02.')
>>>True

pickle.loads(b'\x80\x03X\x01\x00\x00\x00.q\x00h\x00\x86q\x01.')
>>>('.', '.')
pickle.loads(b'\x80\x03X\x01\x00\x00\x00.q\x00X\x01\x00\x00\x00.q\x01\x86q\x02.')
>>>('.', '.')

似乎有重复的相同元素的长而短的腌制版本.

There seems to be a long and short pickled version of tuples with the same element repeatedly.

其他示例:

pickle.loads(b'\x80\x03X\x01\x00\x00\x00#q\x00X\x01\x00\x00\x00#q\x01\x86q\x02.')
>>>('#', '#')
pickle.loads(b'\x80\x03X\x01\x00\x00\x00#q\x00h\x00\x86q\x01.')
>>>('#', '#')

pickle.loads(b'\x80\x03X\x01\x00\x00\x00$q\x00X\x01\x00\x00\x00$q\x01\x86q\x02.')
>>>('$', '$')
pickle.loads(b'\x80\x03X\x01\x00\x00\x00$q\x00h\x00\x86q\x01.')
>>>('$', '$')

我正在尝试按它们的泡菜索引项目,但找不到它们,因为它们的泡菜似乎正在变化.

I'm trying to index items by their pickle but I'm not finding the items because their pickles seem to be changing.

我正在Ubuntu上使用Python 3.3.2.

I'm using Python 3.3.2 on Ubuntu.

推荐答案

腌菜不是唯一的;腌菜不是唯一的.泡菜格式实际上是一种很小的编程语言,以及其他程序(泡菜)可以产生相同的输出(未腌制的对象).从文档中:

Pickles aren't unique; the pickle format is actually a tiny little programming language, and different programs (pickles) can produce the same output (unpickled object). From the docs:

由于pickle数据格式实际上是一种面向堆栈的小型编程语言,并且某些对象的编码具有一定的自由度,因此两个模块[pickle和cPickle]可能为同一输入生成不同的数据流对象.但是,可以保证他们将始终能够读取彼此的数据流.

Since the pickle data format is actually a tiny stack-oriented programming language, and some freedom is taken in the encodings of certain objects, it is possible that the two modules [pickle and cPickle] produce different data streams for the same input objects. However it is guaranteed that they will always be able to read each other’s data streams.

甚至还有一个 pickletools.optimize 函数,该函数需要一个泡菜并输出更好的泡菜您将需要重新设计程序.

There's even a pickletools.optimize function that will take a pickle and output a better pickle. You're going to need to redesign your program.

这篇关于Python泡菜不是一对一的:不同的泡菜给同一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆