In Python, if we have some objects that require persistent storage and do not lose the types and data of our objects, we need to serialize these objects. after serialization, when we need to use them, then we can restore the original data. This process of serialization is called pickle (pickled ). The restoration process is called the reverse pickle. in Python, if we have some objects that require persistent storage and do not lose the types and data of our objects, we need to serialize these objects, after serialization, we can restore the original data when needed. This process of serialization is called pickle (pickled ). The recovery process is called the reverse pickle.
There are two types of pickle for objects: serialization and serialization of objects stored in files.
There is a pickle module in python. we only need to import it.
# Pickle pickling import pickle # import pickle Module # dumps (object) serialize object lista = ["mingyue", "jishi", "you"] # This list is an object, we want to serialize this list object to listb = pickle. dumps (lista) # The dumps () function in the pickle module is used to serialize the object, and then the serialized list is assigned to listb print listb
Output: (lp0 S 'mingyue 'p1 aS 'jishi' p2 aS 'you' p3.
# Load (string) restores the object as it is, and the object type also restores the original format
listc = pickle.loads(listb)
print listc
# Dump (object, file) stores the object in the file for serialization. here it is dump, which is different from the previous dumps by s f1 = file ('1. pkl ', 'WB') pickle. dump (group1, f1, True) f1.close ()
# Data recovery of load (object, file) stored in the file f2 = file ('1. pkl', 'RB') t = pickle. load (f2) print t f2.close ()
Output: ('bajiu', 'wen', 'qingtianc ')