It is often encountered that some strings, lists, dictionaries, and other data are obtained in the Python program running. if you want to save the data for a long time, you can use it later, instead of simply putting the data into the memory and shutting down the power, you will lose the data. The Pickle module in the python module book comes in handy. it can convert objects into a format that can be transmitted or stored. It is often encountered that some strings, lists, dictionaries, and other data are obtained in the Python program running. if you want to save the data for a long time, you can use it later, instead of simply putting the data into the memory and shutting down the power, you will lose the data. The Pickle module in the python module book comes in handy. it can convert objects into a format that can be transmitted or stored.
I. serializing Pickle objects
The Pickle module converts any Python object into a system byte. this operation is called a serialized object.
II. Comparison between Pickle and CPickle
The former is a module fully implemented using Python, and the CPickle is implemented using C, which is much faster than pickle, it is generally recommended that you use CPickle on your computer.
III. dump () method of the Pickle module
There are two common function methods in the Pickle module, one is dump () and the other is load ().
The third part is about the pickle. dump () method:
The syntax of this method is: pickle. dump (object, File, [use protocol])
Tip: the "object" of the data to be persisted is saved to the "file". there are three types of data, with the index 0 being ASCII and 1 being an old binary system, 2 is the new binary protocol. The difference is that the latter is more efficient.
The dump method uses 0 as the protocol by default.
IV. load method of Pickle module
The load () method is opposite to the above dump () method. the above is serialized data. this method is used for deserialization.
Syntax: pickle. load (file)
Tip: Read strings from "files" and convert them to Python data objects in deserialization. you can operate on them normally like operating data types.
For more articles on data object persistence operations in the Python pickle module, refer to the PHP Chinese network!