I have recently got an assignment where I need to put a dictionary (where each key refers to a list) in pickled form. The only problem is I have no idea what pickled form is. Could anyone point me in the right direction of some good resources to help me learn this concept? Thanks!
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
While others have pointed to the Python documentation on the pickle module, which is a great resource, you can also check out Chapter 13: Serializing Python Objects of Dive Into Python 3 by Mark Pilgrim.
Pickling is a mini-language that can be used to convert the relevant state from a python object into a string, where this string uniquely represents the object. Then (un)pickling can be used to convert the string to a live object, by "reconstructing" the object from the saved state founding the string.
What you can see here is that pickle doesn't save the source code for the class, but does store a reference to the class definition. Basically, you can almost read the picked string… it says (roughly translated) "call copy_reg's reconstructor where the arguments are the class defined by
__main__.Foo
and then do other stuff". The other stuff is the saved state of the instance. If you look deeper, you can extract that "string x" is set to "the integer 2" (roughly:S'x'\np6\nI2
). This is actually a clipped part of the pickled string for a dictionary entry… thedict
beingf.__dict__
, which is{'x': 2}
. If you look at the source code forpickle
, it very clearly gives a translation for each type of object and operation from python to pickled byte code.Note also that there are different variants of the pickling language. The default is protocol 0, which is more human-readable. There's also protocol 2, shown below (and 1,3, and 4, depending on the version of python you are using).
Again, it's still a dialect of the pickling language, and you can see that the protocol 0 string says "get a list, include I1, I2, I3", while the protocol 2 is harder to read, but says the same thing. The first bit
\x80\x02
indicates that it's protocol 2 -- then you have]
which says it's a list, then again you can see the integers 1,2,3 in there. Again, check the source code for pickle to see the exact mapping for the pickling language.To reverse the pickling to a string, use load/loads.
Pickling is just serialization: putting data into a form that can be stored in a file and retrieved later. Here are the docs on the
pickle
module:http://docs.python.org/release/2.7/library/pickle.html
The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshaling,” or “flattening”, however, to avoid confusion, the terms used here are “pickling” and “unpickling”.
The pickle module has an optimized cousin called the cPickle module. As its name implies, cPickle is written in C, so it can be up to 1000 times faster than pickle. However, it does not support subclassing of the Pickler() and Unpickler() classes, because in cPickle these are functions, not classes. Most applications have no need for this functionality and can benefit from the improved performance of cPickle. Other than that, the interfaces of the two modules are nearly identical; the common interface is described in this manual and differences are pointed out where necessary. In the following discussions, we use the term “pickle” to collectively describe the pickle and cPickle modules.
The data streams the two modules produce are guaranteed to be interchangeable.
Pickling in Python is used to serialize and de-serialize Python objects, like dictionary in your case. I usually use
cPickle
module as it can be much faster than thePickle
module.The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure.
Pickling - is the process whereby a Python object hierarchy is converted into a byte stream, and Unpickling - is the inverse operation, whereby a byte stream is converted back into an object hierarchy.
Pickling (and unpickling) is alternatively known as serialization, marshalling, or flattening.
To read from a pickled file -
source - https://docs.python.org/2/library/pickle.html