Serialization and deserialization in Python

Source: Internet
Author: User
Tags serialization

Before, in learning Python, always confused the Pickle and JSON module serialization and the use of the inverse of the example, the recent idle time to re-examine the two modules, is basically to understand the difference between them.

Two modules for serialization,

    • JSON, used to convert between string and Python data types
    • Pickle for conversion between Python-specific types and Python data types

The JSON module provides four functions: dumps, dump, loads, load

The Pickle module provides four functions: dumps, dump, loads, load

Look at the following example, maybe you can understand the difference between them

ImportPickledata= ['AA','BB','cc']#dumps converts data into a string that is known only in Python language in a special formP_str =pickle.dumps (data)Print(P_STR)#loads converting pickle data to Python's structureMes =pickle.loads (P_STR)Print(MES)#dump converts the data into a string that is known only in the Python language in a special form and writes the fileWith open ('TMP.PW','WB') as F:pickle.dump (data,f)#load reads data from the data file and transforms it into a python structureWith open ('TMP.PW','RB') as F:Print(Pickle.load (f))

The dump and load operations in JSON and pickle modules are actually the processing of encoding and decoding data.

    • encoding : Converting a python object encoding into a JSON string json.dumps ()
    • decoding : Converting JSON format strings to Python objects json.loads ()

Serialization and deserialization in Python

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.