1.What is Pickle? What are the advantages of Pickle?
Python provides a standard module called Pickle. This is an amazing module that can express almost any Python object (even some Python code blocks (forms)!) as a string, a process called pickling. Reconstructing an object from a string representation is called unpickling. Objects in the encapsulated state can be stored in files or objects, and can also be transferred between remote machines over the network.
The Pickle module will create a Python language-specific binary format without converting them into strings or writing them into a binary file without using underlying file access operations. You basically don’t need to consider any files. detail.
2.Pickle’s main functions
The two main functions in the Pickle module are dump() and load(). The
dump() function saves the data object to a given file in a specific format.
When the load() function takes out saved objects from the file, pickle knows how to restore these objects to their original format.
The dumps() function performs the same serialization as the dump() function. Instead of accepting a 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 reading the serialized data from a file, it accepts a str object containing the serialized data and returns the object directly.
cPickle is a faster C language compiled version of pickle.
pickle and cPickle are equivalent to java's 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 pickle: Pickle your data related articles, please pay attention to PHP Chinese website !