About Python standard library struct serialization and deserialization detailed explanation. This article is enough!

Time:2021-6-4

The text and pictures of this article are from the Internet, only for learning and communication, and do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time

The following article is from Tencent cloud Author: Python house owner

(want to learn Python? Python learning exchange group: 1039649593, to meet your needs, information has been uploaded to the group file stream, you can download! There are also a large number of the latest 2020 Python learning materials.)
在这里插入图片描述

When using Python standard library struct to serialize Python integer, real number and byte string, you need to use the pack() function of struct module to serialize the object in the specified format, and then use the write() method of file object to write the serialized result byte string to the binary file opened in ‘WB’ or ‘ab’ mode. When reading, you need to use the read() method of the file object to read the specified number of byte strings from the binary file opened in ‘RB’ mode, and then use the unpack() function of the struct module to deserialize the original object information. If necessary, you can use the calcsize() function to calculate the number of bytes needed to serialize the specified type. The functions and functions commonly used in the standard library struct are shown in the following table.

Common functions in the table standard library struct
在这里插入图片描述
The struct module supports multiple types of data serialization. The values of the function parameter format in table 9-3 are shown in the following table. These formats can be used in combination to serialize multiple data at the same time. For example, struct.pack (‘ii ‘, 345, 123) means to serialize two integers 345 and 123 into a byte string, and the result is b’y-x01-x00-x00 {x00-x00-x00’.

In addition, format can use the first character to specify the byte order, length and alignment for serialization. If not specified, the default value @ will automatically fill and align bytes as needed. For example, struct. Pack (‘I? ‘), 3, true) is 5 bytes in length, while struct. Pack (‘? The value of I ‘, true, 3) is B’ \ X01 / X00 / X00 / X00 / X03 / X00 / X00 / X00 ‘, and the length is 8 bytes. You can set the first character of format to or =. At this time, byte filling and alignment will not be automatically performed. It should be noted that the byte order and alignment used by the format parameter of the pack() function and the format parameter of the corresponding unpack() function should be consistent.

Format supported by table struct
在这里插入图片描述
Example to write a program to serialize several different values and write them to a binary file, and then read the data in the binary file to restore.

Parsing: the format string ‘if? I56si64si64s’ in the program corresponds to an integer, a real number, a bool value, an integer (representing the number of significant bytes in the 56 bytes immediately after), a byte string (the actual length is 56 bytes, of which only the first part is significant bytes, followed by a filler), and 1 integer (representing the number of significant bytes in the next 64 bytes), 1 byte string (the actual length is 64 bytes, in which only the first part is significant bytes, followed by a filler), 1 integer (representing the number of significant bytes in the next 64 bytes), 1 byte string (the actual length is 64 bytes, the number of significant bytes in the next 64 bytes), and 1 byte string (the actual length is 64 bytes, Only the first part is a valid byte, followed by a filler) for serialization. Through the built-in function str(), any Python object can be converted into a string, and then converted into a byte string by the encode() method, and finally serialized by struct. Struct serialization and deserialization are often used when network programming uses socket to transmit data.
在这里插入图片描述
Results of operation:
在这里插入图片描述

Recommended Today

[rust] basic data type

This article introduces the built-in data types provided by rust. Boolean type Boolean typeLogical values representing yes and No. It has two values:trueandfalseGenerally used in logical expressions, and, or, and not operations can be performed: fn main() { let x = true; let y: bool = ! x; // False, inverse operation let z = […]