Python Pickle Module Data Object Persistence operation

Source: Internet
Author: User
Tags object serialization
Often encountered in the Python program run to get some strings, lists, dictionaries and other data, want to save for a long time, easy to use later, rather than simply put in memory shutdown power loss data. The pickle module in the Python module is useful for converting objects into a format that can be transferred or stored.

First, Pickle object serialization

Pickle module The process of converting any Python object into a system byte is called a serialized object.

Ii. comparison of Pickle and cpickle

The former is completely in Python to implement the module, the Cpickle is implemented with C, it is faster than pickle many times faster, generally recommended if the computer as long as there is cpickle should use it.

Third, the dump () method of the Pickle module

There are 2 common function methods in the Pickle module, one called Dump () and the other called load ().

The third part, play Snake net first to explain to you Pickle.dump () method:

The syntax for this method is: Pickle.dump (object, file, [Usage Protocol])

Hint: The data to be persisted "object", Save to "file", use 3 kinds, index 0 is ascii,1 is old-fashioned 2, 2 is the new 2 binary protocol, the difference is in the latter more efficient.
By default, the dump method uses 0 to do the protocol.

Iv. Load method of the Pickle module

The load () method acts just as opposed to the dump () method above, which is serialized data, which is deserialized.

Syntax: pickle.load (file)

Tip: From the file, read the strings, deserialize them into Python's data objects, and manipulate them as you would normally manipulate the data types.




Related Article

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.