1. What is pickle? What are the advantages of pickle?
Python provides a standard module called Pickle. This is an amazing module that can almost put any Python object (even some Python code blocks (form)!). ) is expressed as a string, and this process is called encapsulation (pickling). The reconstructed object is called a tamper-unpickling from a string expression. Objects in the encapsulated state can be stored in files or objects, or transferred between remote machines over a network.
The pickle module creates a Python-language-specific binary format, without having to convert them to strings, or write them into a binary file without the underlying file access operation, and you don't have to consider any file details.
The main function of 2.Pickle
The two main functions in the Pickle module are dump () and load ().
The dump () function saves the data object in a specific format to a given file.
When the load () function pulls a saved object from a file, pickle knows how to restore those objects to their original format.
The dumps () function performs the same serialization as the dump () function. Instead of accepting the stream object and saving the serialized data to a disk file, this function simply returns the serialized data.
The loads () function performs the same deserialization as the load () function. Instead of accepting a stream object and going to the file to read the serialized data, it takes the object directly back from the Str object that contains the serialized data.
Cpickle is pickle a faster C language compiled version.
Pickle and Cpickle are equivalent to Java serialization and deserialization operations
How to use 3.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
Results:
{' Clio ': 8, ' Alice ': 0}