This article mainly introduces the serialization storage method in Python. the serialization storage mainly targets write operations between memory and hard disk. if you need it, you can refer to the program running process, all variables are in the memory. for example, define a dict:
d = dict(name='Bob', age=20, score=88)
You can modify the variable at any time. for example, you can change the name to 'Bill '. However, once the program ends, all the memory occupied by the variable will be recycled by the operating system. If the modified 'Bill 'is not stored on the disk, the program will be re-run next time and the variable will be initialized to 'Bob '.
The process of changing variables from memory to storage or transmission is called serialization. in Python, it is called pickling. in other languages, it is also called serialization, Alling, flattening, and so on.
After serialization, the serialized content can be written to the disk or transmitted to another machine over the network.
In turn, it is called deserialization to re-read the variable content from the serialized object to the memory, that is, unpickling.
Python provides two modules for serialization: cPickle and pickle. The functions of these two modules are the same. The difference is that cPickle is written in C language and is fast. pickle is written in pure Python and is slow. it is similar to cStringIO and StringIO. When using the command, first try to import cPickle. if it fails, then import the pickle:
try: import cPickle as pickleexcept ImportError: import pickle
First, we try to serialize an object and write it into the file:
>>> d = dict(name='Bob', age=20, score=88)>>> pickle.dumps(d)"(dp0\nS'age'\np1\nI20\nsS'score'\np2\nI88\nsS'name'\np3\nS'Bob'\np4\ns."
The pickle. dumps () method serializes any object into a str, and then writes this str into the file. Alternatively, you can use pickle. dump () to serialize the Object and write it into a file-like Object:
>>> f = open('dump.txt', 'wb')>>> pickle.dump(d, f)>>> f.close()
Look at the dump.txt file, a bunch of messy content, these are the internal information of the object stored in Python.
When we want to read the object from the disk to the memory, we can first read the content into a str, and then use pickle. the loads () method deserializes an object, or you can directly use pickle. the load () method directly deserializes an Object from a file-like Object. We open another Python command line to deserialize the saved object:
>>> f = open('dump.txt', 'rb')>>> d = pickle.load(f)>>> f.close()>>> d{'age': 20, 'score': 88, 'name': 'Bob'}
The variable content is returned!
Of course, this variable is completely irrelevant to the original variable. they are only the same content.
The Pickle problem is the same as the serialization problem exclusive to all other programming languages, that is, it can only be used in Python, and different versions of Python are not compatible with each other. therefore, only Pickle can be used to save unimportant data. it does not matter if deserialization fails.