A brief introduction to the json module and pickle module in Python (with examples)

不言
Release: 2018-10-12 15:52:33
forward
1742 people have browsed it

This article brings you a brief introduction to the json module and pickle module in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The json module and pickle in Python are both used for serialization and deserialization of data. They provide the same methods: dumps, dump, loads, load

  • dumps(obj): Serialize the object to str.

  • dump(obj, fp): Serialize the object to str and store it in the file.

  • loads(s):

    Deserialize the (serialized) string into a Python object.

  • load(fp):
  • Deserialize the (serialized) string

    in the file into a Python object. Although the json and pickle modules are both used for serialization and deserialization of data, there are still many

    differences
  • between them, or each has its own differences Each
Advantages and Disadvantages

:

Universality:
    The string after json serialization is in a universal format (ordinary string) Different platforms and languages ​​can be recognized in , but the pickle-serialized string can only be recognized by Python (Python dedicated serialization module)
  • Processed data types:
  • The objects that json can serialize are only the basic data types in Python, while pickle can serialize all data types in Python.
  • Processed data type:
  • The string after json serialization is text type (you can also understand it after opening the file with Notepad or printing it with print content), and the string serialized by pickle is binary stream data (the content inside is completely incomprehensible after opening Notepad or printing). Therefore, when performing file operations, pay attention to which module is used and whether it needs to be opened in b format.
  • Used space:
  • json requires smaller storage space, while pickle requires larger storage space.
  • The following is a simple example of pickle file operation:

>>> import pickle
>>> dic = {'a': 111, 'b': 222, 'c': 333}
>>> f = open('D:/pk_file.pk', 'wb')
>>> lst = [1, 2, 4, 5]
>>> # 将字典对象和列表对象序列化,并存入文件,文件名后缀自定义为.pk
>>> pickle.dump(dic, f)
>>> pickle.dump(lst, f)
>>> f.close()
>>> # 将文件中的Python对象按写入顺序读取出来,且一次读取一个对象
>>> pk_f = open('D:/pk_file.pk', 'rb')
>>> result = pickle.load(pk_f)
>>> type(result)
<class &#39;dict&#39;>
>>> result
{&#39;a&#39;: 111, &#39;b&#39;: 222, &#39;c&#39;: 333}
>>> other_result = pickle.load(pk_f)
>>> type(other_result)
<class &#39;list&#39;>
>>> other_result
[1, 2, 4, 5]
>>>
Copy after login

The above is the entire content of this article, For more exciting content about python, you can pay attention to the

Python video tutorial

and

python article tutorial

columns on the PHP Chinese website! ! !

The above is the detailed content of A brief introduction to the json module and pickle module in Python (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!