From the zero-learning Python Series A brief talk on the method of encapsulation and unpacking data object of pickle module _python

Source: Internet
Author: User
Tags documentation unpack in python

Encapsulation is a process of converting a Python data object into a byte stream, which is a encapsulated inverse operation that converts bytes in a byte file or byte object into a Python data object and does not unpack data from a trusted source. Can encapsulate and unpack almost any Python data object, mainly including:

None, True,false
Integers, floating-point numbers, complex numbers
String, Byte, ByteArray object
Tuples, lists, collections, dictionaries containing encapsulated objects
Functions defined at the top level of a module
Built-in functions defined at the top level of a module
That is the class defined at the top level of a module
The result of a __dict__ or call to __getstate__ () is an instance of a encapsulated class

The methods commonly used in pickle modules are:

1. Pickle.dump (obj, file, Protocol=none,)

Required parameter obj represents the object that will be encapsulated

The required parameter file represents the file object to which obj will write, and file must be opened in binary writable mode, that is, "WB"

Optional parameter protocol indicates the protocol to be used to inform Pickler, the supported protocols are 0,1,2,3, the default protocol is to add the Protocol 3 in Python 3, other protocol details see the reference documentation

2. Pickle.load (File,*,fix_imports=true, encoding= "ASCII", errors= "strict")

Required parameter file must be opened in binary readable mode, that is, "RB", and all others are optional parameters

3. Pickle.dumps (obj): Returns the encapsulated object as a byte object and does not need to be written into the file

4. Pickle.loads (Bytes_object): reads the encapsulated object from the byte object and returns

There may be three exceptions to the Pickle module:

1. Pickleerror: The exception class that appears when encapsulation and unpacking, inherits from exception

2. Picklingerror: Exception that occurs when encountering an object that is not encapsulated, inherited from Pickleerror

3. Unpicklingerror: The exception that appears in the process of unpacking the object, inherits from Pickleerror

Pickle Application Example:

Copy Code code as follows:

Import Pickle

With open ("My_profile.txt", "WB") as Myprofile:
Pickle.dump ({"Name": "Alwaysjane", "Age": "20+", "Sex": "Female"}, Myprofile)

With open ("My_profile.txt", "RB") as Get_myprofile:
Print (Pickle.load (get_myprofile))

Copy Code code as follows:

Import Pickle

Class Profile:
Name = "Alwaysjane"

Pickledclass = pickle.dumps (Profile)
Print (Pickledclass)
Print (Pickle.loads (pickledclass))

Understanding is not very thorough, hope great God to correct wrong ...

Attached reference documentation

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.