Python's Pickle:pickle your data

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




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.