Home > Backend Development > Python Tutorial > Detailed introduction to json&pickle of python serialization function

Detailed introduction to json&pickle of python serialization function

高洛峰
Release: 2017-03-26 17:27:51
Original
1507 people have browsed it

The json module is a very important module, which can realize cross-platform data exchange between any languages, and can also realize the persistence of some relatively simple data types. (Persistence here means converting some relatively simple data types within Python, such as strings, lists, tuples, dictionaries and other data types, into the standard format of json strings and saving them to the hard disk. )

Commonly used functions of the json module:

json.dumps(): Convert Python’s dictionary-based data types, including (lists, tuples, etc.) into json strings.

json.loads(): Convert json string to a data type recognized by python.

json.dump(): Convert Python’s dictionary-based data types, including (lists, tuples, strings) into json strings, and use the file handle to convert the converted json string Write to file.

json.load(): Read the json string directly from the file through the file handle, and then convert it into a data type recognized by python.

The pickle module only supports data exchange between python programs and can persist some of the more complex data types in python.

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

Commonly used functions of the pickle module:

(The commonly used functions of the pickle module are the same as json)

pickle.dumps(): Python Convert the data type to a special string or byte (note! In the python2.7 version, pickle.dumps will convert the python data type into an unreadable string type. In the python3 or above version, using the pickle.dumps function will directly Convert to bytes. )

pickle.loads(): Used to parse the python data type converted by pickle.

pickle.dump() works the same as dumps, except that it writes directly to the file through the file handle.

pickle.load() reads bytes directly from the file and parses them into data types recognized by python.

Finally summarize the characteristics of the json module and pickle module:

Both json and pickle can achieve data type serialization and persistence functions.

json can do cross-platform (cross-language) data exchange, but pickle cannot. Pickle can only realize data exchange between python and python.

pickle can persist almost all data types in python, including classes, objects, and functions, but json cannot do it. json can only persist some simpler data types, such as strings and lists. , tuple, dictionary, etc.

The above is the detailed content of Detailed introduction to json&pickle of python serialization function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template