Python tutorial: Pickle and cPickle data Python3 built-in modules of persistent Methods Summary
Outline
Module Pickle
implements a binary serialization Python object structure and deserialization. That is, when a Python program continued to run some strings, lists dictionaries, classes and other data objects and even custom requires persistent storage that is stored on disk, to prevent running in memory, loss of data due to power failure. Pickle module then come in handy, it can convert an object into a format that can be transmitted or stored. The python pickle module implements basic data sequence and deserialization. We were able to save through the serialization of objects pickle module runs the program information to a file to permanent storage; deserialization operation by the pickle module, we were able to create the last saved program object from the file.
Compared with JSON module
We can see the pickle module and seemingly similar, but it is essentially different, namely:
-
JSON format is a sequence of text (unicode text output which, although it will be followed by most of the time
utf-8
encoding), the pickle is a binary serialization format; -
JSON is readable literals, rather than pickle (analogies can readability of base64);
-
JSON is interoperable widely used outside the Python system, specific pickle is Python;
Serialization and de-serialization
By reading and writing binary file stored in a targeted manner, using dump
a sequence of data objects, load
deserialize the data object
D = {
'name': 'bob', 'major': { 'english', 'math' }, 'd': [1, 2, 3, 4, 5, 6, 7] } with open('D.pik', 'wb') as f: pickle.dump(D, f) with open('D.pik', 'rb') as f: D = pickle.load(f) print(type(D)) print(D)
Sample results:
<class 'dict'>
{'name': 'bob', 'major': {'english', 'math'}, 'd': [1, 2, 3, 4, 5, 6, 7]}
Of course, we can also serialized into memory (save string format), then the processing object can be transmitted over a network as in any way
pik = pickle.dumps(D)
print(pik) D = pickle.loads(pik) print(type(D)) print(D)
Sample results:
b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x03\x00\x00\x00bobq\x02X\x05\x00\x00\x00majorq\x03cbuiltins\nset\nq\x04]q\x05(X\x07\x00\x00\x00englishq\x06X\x04\x00\x00\x00mathq\x07e\x85q\x08Rq\tX\x01\x00\x00\x00dq\n]q\x0b(K\x01K\x02K\x03K\x04K\x05K\x06K\x07eu.'
<class 'dict'>
{'name': 'bob', 'major': {'english', 'math'}, 'd': [1, 2, 3, 4, 5, 6, 7]}
cPickle
cPickle package of features and usage of the pickle package is almost identical (there is a difference where it is actually rarely used), except that the cPickle is based on the c language, with better performance for most applications, it is recommended to use the module. For the above example, if you want to use cPickle package, we can import statement instead import cPickle as pickle
use.