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.