Python provides a standard module named Pickle. This is an amazing module. it can put almost any Python object (or even some Python code blocks (form )!) Expressed as a string. this process is called pickling ). The re-constructed object expressed from the string is called unpickling ). Objects in the encapsulation status can be stored in files or objects, or transmitted between remote machines over the network. 1. what is Pickle? What are the advantages of Pickle?
Python provides a standard module named Pickle. This is an amazing module. it can put almost any Python object (or even some Python code blocks (form )!) Expressed as a string. this process is called pickling ). The re-constructed object expressed from the string is called unpickling ). Objects in the encapsulation status can be stored in files or objects, or transmitted between remote machines over the network.
The Pickle module will create a binary format dedicated to the Python language, without converting them into strings or writing them into a binary file without the underlying file access operations, basically, you don't need to consider any file details.
2. main functions of Pickle
The two main functions in the Pickle module are dump () and load ().
The dump () function saves data objects to a specified file in a specific format.
When the load () function extracts saved objects from a file, pickle knows how to restore these objects to their original format.
The dumps () function executes the same serialization as the dump () function. Instead of accepting stream objects and saving serialized data to disk files, this function simply returns serialized data.
The loads () function executes the same deserialization as the load () function. Instead of accepting a stream object and reading serialized data from a file, it accepts the str object that contains the serialized data and directly returns the object.
CPickle is pickle to get a faster C language version.
Pickle and cPickle are equivalent to java serialization and deserialization operations.
3. how to use Pickle
Import pickle
With open ('mydata. pickle', 'wb') as mysavedata:
Pickle. dump ({'Alice ': 0, 'Clio': 8}, mysavedata)
With open ('mydata. pickle', 'RB') as myrestoredata:
A_dict = pickle. load (myrestoredata)
Print a_dict
Result:
{'Clio': 8, 'Alice ': 0}
For more Python-related articles on pickle: Pickle your data, please follow the PHP Chinese network!