Python serialization and deserialization of--json and pickle

Source: Internet
Author: User
Tags serialization

First, the definition

Serialization refers to removing an object from memory to write to a store, such as a TXT file, in reverse sequence.

Second, the application scenario

    1. Cross-platform data transfer;
    2. Keep changes to the object the last time you run the program

III. Modules and functions

1. JSON

Dumps (), Dump () (serialized)

Loads (), load () (deserialization)

2. Pickle

Dumps (), Dump () (serialized)

Loads (), load () (deserialization)

Iv. Application Cases

1. Pickle

1 ImportPickle2 3info = {"Lyrics":"Geci",4         "Song":"gequ",5         " Time": 89}6 7With open ("Pickling.txt","WB") as F:#Serialization of8 F.write (Pickle.dumps (info))9 TenWith open ("Pickling.txt","RB") as F:#deserialization One         Print(Pickle.load (f))

The above code first converts the dictionary info to an object of type Str and then writes to the TXT file, where line 8th can be replaced with

Pickle.dump (INFO,F)

The two implementations are the same, except that dums directly serializes the object and writes it to the TXT file. The result of writing a file is a bunch of things that you don't understand, as follows:

But this is not an error, not a letter, and finally the load () function to deserialize it after the console output is:

Similarly, the loads () function can replace the load function. As follows:

1 print(Pickle.loads (F.read ()))

2. JSON

Pickle is unique to Python, and JSON enables data conversion and transmission in different programming languages. Data transfer in different programming languages requires the conversion of their data types to a common standard type, and JSON is one of them. The corresponding relationship between the JSON data type and the Python data type is as follows:

The use of JSON is roughly the same as pickle, except that the read-write mode of JSON is not "WB" and "RB", but "W" and "R" when reading and writing files.

Python serialization and deserialization of--json and pickle

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.