Serialization and deserialization of day14 python learning

Posted by the max on Fri, 17 Dec 2021 10:44:36 +0100

catalogue

Introduction to serialization and deserialization

json module

pickle module

Compare the difference between json module and pickle module

Introduction to serialization and deserialization

Through file operation, we can write strings to a local file. However, if it is an object (such as list, dictionary, tuple, etc.), it cannot be written directly to a file. The object needs to be serialized before it can be written to the file.

Design a set of protocols to convert the data in memory into byte sequence and save it to the file according to some rules, which is serialization. On the contrary, recovering from the byte sequence of the file to memory is deserialization

In short:

Serialization: the process of persisting data from memory to hard disk

Deserialization: the process of loading data from hard disk into memory

Python provides json and pickle modules to serialize and deserialize data

json module

There are two ways to persist data in json:

dumps: converts data into json strings and does not save data to files

dump: write the data to the specified file while converting it into a json string

import json

name = ['Jack', 'Merry', 'Tom', 'Jackson']
json_name = json.dumps(name) # Converts data to a string without writing to a file
print(json_name, type(json_name))
file = open('name.txt', 'w', encoding='utf8')
json.dump(name, file) # Write name while converting the data to a string Txt file
file.close()

json deserialization also has two methods:

loads: load json strings as data in python

Load: read the file and load the read content into data in python

json module:

  1. Convert objects into strings. Strings are recognizable in any operating system or programming language
  2. json is used to transfer data between different platforms
  3. Not all objects can be directly converted into a string (such as an instance object created by a class written by yourself). The following table lists the corresponding relationship between python objects and json strings
PythonJSON
dictobject
list,tuplearray
strstring
int,floatnumber
Truetrue
Falsefalse
Nonenull
  • If it is a custom object, it cannot be converted to json string by default. You need to manually specify JSONEncoder
  • If you convert a json string back into an object, the methods in the object cannot be used                     

pickle module

Storing data in python only supports storing string and binary

json: convert the data in python (str/list/tuple/dict/int/float/bool/None) into corresponding json

pickle: convert any object in python to binary

pickle module serialization:

dumps: convert python data to binary

dump: converts python data to binary and saves it to a specified file

pickle module deserialization:

Loads: loads binary data into python data

Load: read the file and load the binary contents of the file into python data

import pickle

name = ['Jack', 'Merry', 'Tom', 'Jackson']
# Use of serialized dumps
name_dumps = pickle.dumps(name) # Convert python data to binary
print(name_dumps, type(name_dumps))
# Use of serialized dump
file = open('name_pickle.txt', 'wb')
pickle.dump(name, file) # Convert python objects to binary and write to a file (there will be garbled code)
file.close()

# Use of deserialized loads
name_loads = pickle.loads(name_dumps)
print(name_loads, type(name_loads))
# Use of deserialization
file1 = open('name_pickle.txt', 'rb')
name_load = pickle.load(file1)
print(name_load, type(name_load))
file1.close()

pickle module is used for conversion of any data type

Examples are as follows: (convert the instance object created by the user-defined class)

import pickle

class Student(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def study(self):
        print(self.name + 'Studying hard...')

stu = Student('Jack', 18)
# Use of serialized dumps
stu_dumps = pickle.dumps(stu) # Convert instance object to binary
print(stu_dumps, type(stu_dumps))
# Use of serialized dump
file = open('student.txt', 'wb')
pickle.dump(stu, file) # It is normal to convert the instance object to binary and write it to a file, but there is still garbled code
file.close()

# Use of deserialized loads
stu_loads = pickle.loads(stu_dumps) # Converts the binary of an instance object intact to an instance object
print('full name:{}, Age:{}'.format(stu_loads.name, stu_loads.age))
stu_loads.study() # After being converted to an instance object, the properties and methods of the object are still available
# Use of deserialization
file1 = open('student.txt', 'rb')
stu_load = pickle.load(file1)
print('full name:{}, Age:{}'.format(stu_load.name, stu_load.age))
stu_load.study() # After being converted to an instance object, the properties and methods of the object are still available
file1.close()

Compare the difference between json module and pickle module

json can only save part of the information, which is used to transfer data on different platforms

The data stored in json are all basic data types

pickle is used to convert data intact into binary, but this binary can only be recognized in Python

Topics: Python Pycharm computer