Serialization and deserialization of Python Development: pickle, json module Explanation

1 Introduction

  Serialization: converting the non-persistent memory and easy to transfer persistent objects and object transmission process.

  Deserialize: persistent and will be transmitted to non-persistent object into the object and the transmission process.

  Python is provided pickle and json two modules to implement the serialization and deserialization, pickle module and the json module dumps (), dump (), loads (), load () which is a function, which dumps (), dump () used to implement serialization, loads (), load () for implementing the deserialization. Here, we are on the pickle and json modules are introduced.

2 pickle module

  dumps pickle module (), dump (), loads (), load () is a function can be functionally divided into two groups:
  Serialization: dumps (), dump ()
  Deserialize: loads (), load ()
   Difference dumps () and dump () is dumps () simply to give the target sequence, and the dump () will be after the sequence of file write result to them; corresponding, loads () and load () difference As loads () is a serialization of the dumps are deserialized, and the dump () reads the contents of the file from the deserialized.

2.1 dumps () and loads ()

>>> Import the pickle
 >>> p_dict = { ' name ' : ' John Doe ' , ' Age ' : 30, ' isMarried ' : False} # define a dictionary 
>>> = p_str the pickle.dumps (p_dict) # serialized 
>>> type (p_dict)
 < class  ' dict ' > 
>>> type (p_str)
 < class  ' bytes ' > 
>>> p_str 
B '\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x06\x00\x00\x00\xe5\xbc\xa0\xe4\xb8\x89q\x02X\x03\x00\x00\x00ageq\x03K\x1eX\t\x00\x00\x00isMarriedq\x04\x89u.'
>>> p = pickle.loads(p_str)
>>> type(p)
<class 'dict'>
>>> p
{'name': '张三', 'age': 30, 'isMarried': False}
  We can see, p_dict content before p and serialization after deserialization get is exactly the same. However, p and p_dict already are two different objects: the
>>> id(p)==id(p_dict)
False

2.2 dump()与load()

   Serialization:
>>> Import the pickle
 >>> p_dict = { ' name ' : ' John Doe ' , ' Age ' : 30, ' isMarried ' : False} # define a dictionary 
>>> = Open File ( " my_dump.txt " , " wb " ) # because serialization is the only type of bites, it must be opened wb mode 
>>> the pickle.dump (p_dict, File)
 >>> File.close ()
  In this case, the above code we have p_dict sequence of success, and written to a file named my_dump.txt. You can find this file, then copy it to any computer on deserialize:
>>> file=open("my_dump.txt","rb")
>>> p=pickle.load(file)
>>> file.close()
>>> type(p)
<class 'dict'>
>>> p
{'name': '张三', 'age': 30, 'isMarried': False}
  Look, the content before the content and after serialization deserialization get exactly the same. Appreciate the serialization and de-serialization of action yet? After serialized content can easily get saved to disk, the computer shut down is not afraid.

3 json module

  If you read and understand the contents of the above sectors on pickle and json modules for the contents of this section, you can effortlessly grasp. Above spoke of, and pickle as, JSON module also provides the dumps (), dump (), loads (), load () it is a function, and wherein the difference also be a pickle distinguish functions are the same.

3.1 dumps () and loads ()

>>> Import the pickle
 >>> p_dict = { ' name ' : ' John Doe ' , ' Age ' : 30, ' isMarried ' : False} # define a dictionary 
>>> Import JSON
 >>> p_dict = { ' name ' : ' John Doe ' , ' Age ' : 30, ' isMarried ' : False} # define a dictionary 
>>> p_str = JSON.
dumps(p_dict)
>>> type(p_str)
<class 'str'>
>>> p_str
'{"name": "\\u5f20\\u4e09", "age": 30, "isMarried": false}'
  Can be seen, obtained after json json serialization format is a string, the foregoing json string, Chinese content display section for "garbage." How to do it? json the dumps () function (the dump () function also has) provided a ensure_ascii parameter, the value of the parameter set to False, the sequence of the Chinese can still make normal display.
>>> p_str2 = json.dumps(p_dict, ensure_ascii=False)
>>> p_str2
'{"name": "张三", "age": 30, "isMarried": false}'
  Following the above content deserialized:
>>> p1 = json.loads(p_str)
>>> p1
{'name': '张三', 'age': 30, 'isMarried': False}
>>> p2 = json.loads(p_str)
>>> p2
{'name': '张三', 'age': 30, 'isMarried': False}

3.2 dump()与load()

>>> import json
>>> p_dict = {'name':'张三' , 'age':30 , 'isMarried':False} # 定义一个字典
>>> file = open('d:/mydump.txt' , 'w')
>>> json.dump(p_dict , file)
>>> file.close()
  Of course, you can also add ensure_ascii this parameter, and set its value to False, so you open the file mydump.txt inside the Chinese will be able to display properly. (After completing the implementation of the code, there will be a mydump.txt local file, you can verify that the content of you)
>>> file = open('d:/mydump.txt' , 'w')
>>> json.dump(p_dict , file , ensure_ascii=False)
>>> file.close()
  Continue deserialization:
>>> file = open('d:/mydump.txt' , 'r')
>>> p = json.load(file)
>>> file.close()
>>> type(p)
<class 'dict'>
>>> p
{'name': '张三', 'age': 30, 'isMarried': False}
  Through the above content, pickle and json serialization module is operated on and deserialization introduced over it. We can find, pickle and json two modules both in the name of the function, or in function, are similar machines. In this case, with the pickle module, why are born json module it? Then he said the difference between pickle and json modules.

4 pickle difference between the module and the module json

   (1) pickle Python language-specific module types and user-defined types between the Python and the basic data type conversion
  json module for converting data types between the string and the python. As shown below, our custom a Person class, are serialized with pickle and json:
>>> class Person:
def __init__(self , name , age , isMarried):
self.name = name
self.age = age
self.isMarried = isMarried
>>> p = Person('张三' , 30 , False)
  Pickle module using serialization and deserialization:
>>> p = Person('张三' , 30 , False)
>>> import pickle
>>> pp = pickle.dumps(p)
>>> type(pp)
<class 'bytes'>
>>> pp
b'\x80\x03c__main__\nPerson\nq\x00)\x81q\x01}q\x02(X\x04\x00\x00\x00nameq\x03X\x06\x00\x00\x00\xe5\xbc\xa0\xe4\xb8\x89q\x04X\x03\x00\x00\x00ageq\x05K\x1eX\t\x00\x00\x00isMarriedq\x06\x89ub.'
>>> p2 = pickle.loads(pp)
>>> type(p2)
<class '__main__.Person'>
>>> p2.name
' John Doe '
  Even pickle module can also serialize itself Peron:
>>> per = pickle.dumps(Person)
>>> per
b'\x80\x03c__main__\nPerson\nq\x00.'
>>> per2 = pickle.loads(per)
>>> per2
<class '__main__.Person'>
  If the serialize the object with the Person instance json, will be given:
>>> import json
>>> p = Person('张三' , 30 , False)
>>> json.dumps(p)
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
json.dumps(p)
……
TypeError: Object of type 'Person' is not JSON serializable
   If you have to serialize the Person object with json, you must define a method of conversion of the Person object dictionary (dict) of :
>>> def person2dict(per):
return {
'name':per.name ,
'age':per.age ,
'isMarried':per.isMarried
}
>>> p3 = json.dumps(p , default=person2dict)
>>> type(p3)
<class 'str'>
>>> p3
'{"name": "\\u5f20\\u4e09", "age": 30, "isMarried": false}'
>>> p3 = json.dumps(p , default=person2dict , ensure_ascii=False)
>>> type(p3)
<class 'str'>
>>> p3
'{"name": "张三", "age": 30, "isMarried": false}'
  Of course, it can not directly deserialized, otherwise it will only get a dictionary:
>>> p4 = json.loads(p3)
>>> type(p4)
<class 'dict'>
>>> p4
{'name': '张三', 'age': 30, 'isMarried': False}
  At this time, also define a dictionary to convert the Person class instance method, during deserialization:
>>> def dict2person(d):
return Person(d['name'],d['age'],d['isMarried'])
>>> p5 = json.loads(p3 , object_hook=dict2person)
>>> type(p5)
<class '__main__.Person'>
>>> p5.name
'张三'
  (2) pickle serialization of the type of bites, only suitable for interaction between the Python machine.
    str json serialization result type, can be identified in multiple languages, it may be used to interact with other programming languages.
  Currently, JSON format string has become a standard format of the network transmission, it is usually used to serialize json module in the web instead of background development pickle module.
   JSON Python and the corresponding built-in data types as follows:
JSON type
Python type
{}
dict
[]
list
"string"
'Str' or u'unicode '
1234.56
int or float
true/false
True/False
null
None

5 summary

  (1) serialized and deserialized in order to solve the transmission problem of persistent objects in memory;
  (2) Python provides two modules pickle and json serialization and deserialization;
  (3) dumps () and dump () for serialization, loads () and load () for deserialization;
  (4) pickle module can serialize any object type serialization bites result, only suitable for interaction between the Python machine;
  json serialization module only Python basic types, results json serialized format string, suitable for interaction between the different programming languages.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160162.htm