Python object-Pickle pickling

Source: Internet
Author: User
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 ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.