Python3 Jason, Pickle and Cpickle

Source: Internet
Author: User

Python JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for people to read and write. The JSON data format is actually the dictionary format in Python, which can contain arrays enclosed in square brackets, which is the list in Python.

In Python, there are modules--jason and pickle that specialize in the Jason format.

The Jason module offers four methods: dumps, dump, loads, load

The Pickle module also offers four functions: dumps, dump, loads, load

First, dumps and dump

Dumps and dump serialization methods

Dumps only completed serialization of STR

Dump must pass a file descriptor to save the serialized str in the file

File operation:

Second, loads and load

Loads and load deserialization methods

Loads only the deserialization is complete.

Load only accepts file descriptors, finishes reading files and deserializes

Third, Jason and pickle modules

Both the Jason module and the Pickle module have dumps, dump, loads, and load four methods, and use the same

The difference is that the JSON module is serialized out as a common format, and other programming languages are known as ordinary strings

and the Pickle module serialized out only Python can be recognized, other programming languages do not know, the performance of garbled

However, Pickle can serialize functions, but other files want to use the function, where the definition of the file needs to be defined (definitions and parameters must be the same, content can be different)

Relationship of four Python objects to JSON objects

Five summary

1. JSON serialization method

Dumps: No file operation dump: Serialization + Write file

2, Jason deserialization method:

Loads: No file operation load: Read file + deserialization

3, JSON module serialization of data more general

The Pickle module serializes data only Python is available, but is powerful and can serialize functions.

4. The JSON module can serialize and deserialize data types as shown in the table above

5. Format write file using indent=4

Cpckle

The Python standard library provides pickle and cpickle modules. Cpickle is encoded in C and is higher in efficiency than pickle , but the type defined in the Cpickle module cannot be inherited (most of the time, we do not need to inherit from these types, we recommend using Cpickle). The serialization/deserialization rules for cpickle and Pickle are the same, using pickle to serialize an object that can be deserialized using Cpickle . At the same time, these two modules become more "smart" when dealing with self-referencing types, and it does not have unrestricted recursive serialization of self-referencing objects, which are serialized only once for multiple references to the same object.

Python3 Jason, Pickle and Cpickle

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.