1. What is serialization?

While the program is running, all variables or objects are stored in memory. Once the program is called, the memory occupied by these variables or objects is reclaimed. In order to realize persistent storage of variables and objects to disk or transfer over the network, we need to convert variables or objects into binary streams. The process of converting it to a binary stream is serialization.

2. What is deserialization

Deserialization means that the program cannot read from disk when it runs. Serialized objects or variables need to be moved from disk to memory, and the binary stream is converted to its original data format. We call this process deserialization.

3. How does Python implement serialization and deserialization

There are two modules here: pickle module and JSON module

3.1 the pickle module

Dumps (object) and dumps(objects) are serialized loads(byte) and loads(byte) are deserialized

import pickle

dict1 = {'a': 1, 'b': 2}
dict2 = pickle.dumps(dict1)
print(dict2,type(dict2),id(dict2))
dict3 = pickle.loads(dict2)
print(dict3,type(dict3),id(dict3))
Copy the code

D:\python\Python36\python.exe E:/demo/-python-/handlers/test.py b'\x80\x03}q\x00(X\x01\x00\x00\x00aq\x01K\x01X\x01\x00\x00\x00bq\x02K\x02u.' <class 'bytes'> 1536157390816 {'a': 1, 'b': 2} <class 'dict'> 1536043176080

The output shows that the address in memory is different because after serializing the dictionary, the end of the dictionary call is actually released in memory, and after deserializing the dictionary, it is removed from disk and redivided in memory, so the address is displayed differently.

Dump and load serialize and deserialize files

import pickle
import os

dict1 = {'a': 1, 'b': 2}
file = open(os.path.join(os.path.abspath('. '), '1.txt'), 'wb')
dict2 = pickle.dump(dict1, file)
file.close()
print(dict2, type(dict2), id(dict2))
file1 = open(os.path.join(os.path.abspath('. '), '1.txt'), 'rb')
dict3 = pickle.load(file1)
file1.close()
print(dict3, type(dict3), id(dict3))
Copy the code

None <class 'NoneType'> 1805423824 {'a': 1, 'b': 2} <class 'dict'> 2622050204264

3.2 json module

Json. dumps: loads are being converted from Python to json string formats. Loads are being converted from JSON string formats to Python data formats

import json

dict1 = {'a': 1, 'b': 2}
dict2 = json.dumps(dict1)
print(dict2, type(dict2), id(dict2))
dict3 = json.loads(dict2)
print(dict3, type(dict3), id(dict3))
Copy the code

{"a": 1, "b": 2} <class 'str'> 2040970141536 {'a': 1, 'b': 2} <class 'dict'> 2040937894392

Json. dump: serializes the file to json.load: deserializes the content in the file

import os

dict1 = {'a': 1, 'b': 2}
file1 = open(os.path.join(os.path.abspath('. '), '1.txt'), 'w')
dict2 = json.dump(dict1, file1)
file1.close()
print(dict2, type(dict2), id(dict2))
file2 = open(os.path.join(os.path.abspath('. '), '1.txt'), 'r')
dict3 = json.load(file2)
print(dict3, type(dict3), id(dict3))
Copy the code

None <class 'NoneType'> 1805423824 {'a': 1, 'b': 2} <class 'dict'> 1907991182648

Reference: blog.csdn.net/reuxfhc/art… Blog.csdn.net/u012993796/…