A detailed description of the json&pickle of the Python serialization feature

Source: Internet
Author: User
The JSON module is a very important module that enables cross-platform data exchange between any language, and enables the persistence of some relatively simple data types. (The persistence here is to put Python's simple data types, such as strings, lists, tuples, dictionaries and the like, into the standard format of the JSON string, saved to the hard disk.) )

JSON module Common functions:

Json.dumps (): Converts Python's dictionary-based data types, including (lists, tuples, and so on) to JSON strings.

Json.loads (): Converts a JSON string to a data type that Python can recognize.

Json.dump (): Converts a Python dictionary-based data type, including (list, tuple, string) to a JSON string, and writes the converted JSON string to a file through a file handle.

Json.load (): Read the JSON string directly from the file with a file handle and convert it to a data type that Python can recognize.

The pickle module, which only supports data exchange between Python programs, can persist some of the more complex data types in Python.

(Pickle can not only save dictionaries, lists, tuples, and other simple data types to the hard disk, but also can persist some more complex data types, such as functions, classes, objects, etc. can be stored to the hard disk!) )

Common functions of the Pickle module:

(The functions commonly used by pickle modules are the same as JSON)

Pickle.dumps (): Converts data types in Python to special strings or bytes (note!) In the python2.7 version pickle.dumps converts the Python data type to an unreadable string type, and the Pickle.dumps function is converted directly to bytes bytes in versions above Python3. )

Pickle.loads (): Used to parse the Python data type that was converted by pickle.

Pickle.dump () acts like dumps, except that it is written directly to a file through a file handle.

Pickle.load () reads the bytes directly from the file, parsing it into a data type that Python can recognize.

Finally, we summarize the features of the JSON module and the Pickle module:

Both JSON and Pickle are capable of serializing and persisting data types.

JSON can do cross-platform (cross-language) data exchange, pickle cannot, pickle can only implement data exchange between Python and Python.

Pickle can almost persist all data types in Python, classes, objects, functions, but JSON does not, JSON can only persist some relatively simple data types, such as strings, lists, tuples, dictionaries, etc.

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.