Python Library: Serialization and deserialization module pickle Introduction

Source: Internet
Author: User

1 Preface

The use of the Pickle library is mentioned in the article "Understanding what is machine learning through a simple example", which is described in this paper.

    • A simple example to understand what machine learning is


Pickle is a standard module in the Python language, and after installing Python, the Pickle Library is included and does not need to be installed separately. The
Pickle module implements basic data serialization and deserialization. Through the serialization of the Pickle module we are able to save the object information running in the program to a file, to store it permanently, and through the Pickle module's deserialization, we are able to create from the file the last object saved by the program.
in the official introduction, there are several words in the English description of the serialization operation, such as "serializing", "pickling", "serialization", "marshalling" or "flattening", etc. They all represent the meaning of serialization. Accordingly, the anti-serialization operation of the English word also has a number of good, such as "de-serializing", "unpickling", "deserailization" and so on. To avoid confusion, the "pickling"/"unpickling", or "serialization"/"deserailization" is generally used.
pickle module is serialized in binary form and saved to a file (the file suffix ". Pkl") cannot be opened directly for previewing. Another serialization standard module of Python, json , it is human-readable, you can open the view directly (for example, in notepad++ view).

The Pickle module has two main types of interfaces, serialization and deserialization.
Among the serialization operations are:

  • pickle.dump()
  • Pickler(file, protocol).dump(obj)
    Deserialization operations include:
  • pickle.load()
  • Unpickler(file).load()
2 Serialization Operation 2.1 Serialization Method Pickle.dump ()

To serialize the method pickle.dump() , the parameters of the method are as follows:
Pickle.dump (obj, file, protocol=none,*,fix_imports=true)
The method is implemented to save the serialized object, obj, in binary form to file. Its function is equal to Pickler(file, protocol).dump(obj) .
With regard to the parameter file, it is important to note that the operation must be done in binary form (write).
Refer to the previous case as follows:

import picklewith open(‘svm_model_iris.pkl‘‘wb‘as f:    pickle.dump(svm_classifier, f)

File is ' Svm_model_iris.pkl ' and is written in binary form (' WB ').

For the parameter protocol, there are 5 different types, i.e. (0,1,2,3,4). (0,1,2) corresponds to the earlier version of Python, and (3,4) is the version after Python3.
In addition, the parameters are optional pickle. Highest_protocol and Pickle.default_protocol. Currently, the value of Pickle.highest_protocol is 4,pickle in the python3.5 version. The value of Default_protocol is 3. When the protocol parameter is negative, the selected parameter is pickle. Highest_protocol.
For the parameter protocol, the official details are as follows:

2.2 Serialization Method Pickle.dumps ()

pickle.dumps () method are as follows:
Pickle.dumps (obj, protocol=none,*,fix_imports=true)
pickle.dumps () method with pickle.dump () method is that pickle.dumps () method does not need to be written to the file, it returns a serialized bytes object directly.

2.3 Serialization Method Pickler (file, protocol). Dump (obj)

The Pickle module provides a serialized object-oriented class method, that is class pickle.Pickler(file, protocol=None,*,fix_imports=True) , the Pickler class has a dump () method.
Pickler (file, protocol). Dump (obj) implements the same functionality as Pickle.dump ().
Please refer to the official API for the other method of the Pickler class.

A hard-line: the technical article forwards too much, this article comes from the public number: "The path of Python data" (id:pydataroad).

3 Deserialization Operation 3.1 deserialization method Pickle.load ()

pickle.load () , the relevant parameters of the method are as follows:
Pickle.load (file, *,fix_imports=true, encoding= "ASCII". errors= "strict")
The method is implemented to read the serialized object from the file. Its function is equivalent to unpickler (file). Load () .
With regard to the parameter file, it is important to note that the operation must be done in binary form (read).
Refer to the previous case as follows:

import picklewith open(‘svm_model_iris.pkl‘‘rb‘as f:    model = pickle.load(f)

File is ' svm_model_iris.pkl ' and is read in binary form (' RB ').

When reading, the parameter protocol is automatically selected, and the load () method does not have this parameter.

3.2 Deserialization Method Pickle.loads ()

pickle.loads()The parameters of the method are as follows:
Pickle.loads (Bytes_object, *,fix_imports=true, encoding= "ASCII". errors= "Strict")
pickle.loads()Method andpickle.load()The difference between methods is thatpickle.loads()The method is to read the serialized information directly from the bytes object, rather than from the file.

3.3 Deserialization Method Unpickler (file). Load ()

The Pickle module provides a deserialized object-oriented class method, that is class pickle.Unpickler(file, *,fix_imports=True, encoding="ASCII". errors="strict") , the Pickler class has the load () method.
Unpickler (file). Load () implements the same functionality as Pickle.load ().
Please refer to the official API for the other method of the Unpickler class.

4 Those types can be serialized and deserialized

This is the official document, which I will not describe further here.

Write it in the back.

Pickle module is still more practical, of course, about the Pickle module, in fact, there is a lot of information to understand, want to learn more information about the children's shoes, it is recommended to read the official Python API document (library file).

?

Python Library: Serialization and deserialization module pickle Introduction

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.