Python pickle: Pickleyourdata

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

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.