The struct module provides functions for converting between byte strings and Pytdon's native data types, such as numbers and strings.
The role of this module is to perform conversions between Pytdon values and Pytdon string forms of C language structures. This can be used to process binary data stored in files or from network connections, as well as other data sources.
1. Module functions and Struct classes
In addition to providing a Struct class, it has a number of module-level functions for working with structured values. There is a notion of Format specifiers, which refers to the conversion from a string format to a compiled representation, similar to the way regular expressions are handled. It is often more efficient to instantiate the Struct class and call the class method to accomplish the conversion than to call the module function directly.
The following examples all use the Struct class.
2. Packing and unpacking
Struct supports packing data into strings, and can reverse unpacking (unpacking) data from strings.
In this example, the specifier takes an integer or long integer, a two-byte string, and a floating-point number. Formatting characters in the space used to separate the indicators (indicators), in the compilation of the format will be ignored.
import struct import binascii values = (1, 'ab'.encode('utf-8'), 2.7) s = ('I 2s f') packed_data = (*values) print('Original value:', values) print('Formatting characters:', ) print('Occupied bytes:', ) print('Packaging results:', (packed_data))
Output:
Original value: (1, b'ab', 2.7)
Format symbol: b'I 2s f'
Bytes occupied: 12
Packaging result: b'0100000061620000cdcc2c40'
This example converts the packed value to a sequence of hexadecimal bytes, which is printed out using the () method.
Use the unpack() method to unpack.
import struct import binascii packed_data = (b'0100000061620000cdcc2c40') s = ('I 2s f') unpacked_data = (packed_data) print('Unpacking results:', unpacked_data)
Output:
Unpacking result: (1, b'ab', 2.700000047683716)
Passing the packed value to unpack() essentially returns the same value (floating point numbers will vary).
3. Byte order/size/alignment
By default, pack is encoded using the byte order of the native C library. The first character of the formatting string can be used to indicate the byte order, size, and alignment of the padding data, as described in the following table.
Character | Byte order | Size | Alignment |
@ | this locality | this locality | this locality |
= | this locality | standard | none |
< | little-endian (small-letter byte order) | standard | none |
> | big-endian (big-endian) | standard | none |
! | network (= big-endian) | standard | none |
If these are not set in the formatter, then @ will be used by default.
Native byte order means that the byte order is determined by the current host system. For example: Intel x86 and AMD64 (x86-64) use little-endian byte order; Motorola 68000 and PowerPC G5 use big-endian byte order.ARM and Intel Anthem support toggle byte order. You can use to view the byte order of the current system.
The local size (Size) and alignment are determined by the c compiler's sizeof expression. It corresponds to the local byte order.
The standard size is determined by the formatting symbols, and the standard size of each format is described below.
Example.
4. Format symbols
The table of formatting characters is as follows.
5. Buffers
Packaging data into binary is usually used in scenarios where performance is critical.
This can be optimized in such scenarios by avoiding the overhead of allocating new buffers for each packed structure.
The pack_into() and unpack_from() methods support direct writes to pre-allocated buffers.
import array import binascii import ctypes import struct s = ('I 2s f') values = (1, 'ab'.encode('utf-8'), 2.7) print('Original value:', values) print() print('Use ctypes module string buffer') b = ctypes.create_string_buffer() print('Raw buffer :', ()) s.pack_into(b, 0, *values) print('Packing results are written to :', ()) print('Unpacking :', s.unpack_from(b, 0)) print() print('Using the array module') a = ('b', b'\0' * ) print('Original value :', (a)) s.pack_into(a, 0, *values) print('Packing and writing :', (a)) print('Unpacking :', s.unpack_from(a, 0))
Output:
Original value: (1, b'ab', 2.7)
Using the ctypes module string buffer
Original buffer : b'0000000000000000000000000000'
Packing result written : b'0100000061620000cdcc2c40'
Unpack : (1, b'ab', 2.700000047683716)
Using the array module
Raw value : b'0000000000000000000000000000'
Packing write : b'0100000061620000cdcc2c40'
Unpack : (1, b'ab', 2.700000047683716)
to this article on the Python module learning struct module detailed article is introduced to this, more related Python struct module content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!