struct
— 将字节解释成打包二进制数据
¶
源代码: Lib/struct.py
This module converts between Python values and C structs represented as Python
bytes
objects. Compact
格式字符串
describe the intended conversions to/from Python values. The module’s functions and objects can be used for two largely distinct applications, data exchange with external sources (files or network connections), or data transfer between the Python application and the C layer.
注意
When no prefix character is given, native mode is the default. It packs or unpacks data based on the platform and compiler on which the Python interpreter was built. The result of packing a given C struct includes pad bytes which maintain proper alignment for the C types involved; similarly, alignment is taken into account when unpacking. In contrast, when communicating data between external sources, the programmer is responsible for defining byte ordering and padding between elements. See 字节序、大小和对齐 了解细节。
几个
struct
函数 (和方法对于
Struct
) 接受
buffer
argument. This refers to objects that implement the
缓冲协议
and provide either a readable or read-writable buffer. The most common types used for that purpose are
bytes
and
bytearray
, but many other types that can be viewed as an array of bytes implement the buffer protocol, so that they can be read/filled without additional copying from a
bytes
对象。
函数和异常 ¶
该模块定义了下列异常和函数:
- exception struct. error ¶
-
Exception raised on various occasions; argument is a string describing what is wrong.
- struct. pack ( format , v1 , v2 , ... ) ¶
-
Return a bytes object containing the values v1 , v2 , … packed according to the format string format . The arguments must match the values required by the format exactly.
- struct. pack_into ( format , buffer , offset , v1 , v2 , ... ) ¶
-
Pack the values v1 , v2 , … according to the format string format and write the packed bytes into the writable buffer buffer 起始于位置 offset 。注意, offset is a required argument.
- struct. unpack ( format , buffer ) ¶
-
Unpack from the buffer buffer (presumably packed by
pack(format, ...)) according to the format string format . The result is a tuple even if it contains exactly one item. The buffer’s size in bytes must match the size required by the format, as reflected bycalcsize().
- struct. unpack_from ( format , / , buffer , offset = 0 ) ¶
-
Unpack from buffer 起始于位置 offset , according to the format string format . The result is a tuple even if it contains exactly one item. The buffer’s size in bytes, starting at position offset , must be at least the size required by the format, as reflected by
calcsize().
- struct. iter_unpack ( format , buffer ) ¶
-
Iteratively unpack from the buffer buffer according to the format string format . This function returns an iterator which will read equally sized chunks from the buffer until all its contents have been consumed. The buffer’s size in bytes must be a multiple of the size required by the format, as reflected by
calcsize().Each iteration yields a tuple as specified by the format string.
Added in version 3.4.
- struct. calcsize ( format ) ¶
-
Return the size of the struct (and hence of the bytes object produced by
pack(format, ...)) corresponding to the format string format .
格式字符串 ¶
Format strings describe the data layout when packing and unpacking data. They are built up from format characters , which specify the type of data being packed/unpacked. In addition, special characters control the byte order, size and alignment . Each format string consists of an optional prefix character which describes the overall properties of the data and one or more format characters which describe the actual data values and padding.
字节序、大小和对齐 ¶
By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler). This behavior is chosen so that the bytes of a packed struct correspond exactly to the memory layout of the corresponding C struct. Whether to use native byte ordering and padding or standard formats depends on the application.
Alternatively, the first character of the format string can be used to indicate the byte order, size and alignment of the packed data, according to the following table:
|
字符 |
字节序 |
Size |
对齐 |
|---|---|---|---|
|
|
native |
native |
native |
|
|
native |
standard |
none |
|
|
little-endian |
standard |
none |
|
|
big-endian |
standard |
none |
|
|
network (= big-endian) |
standard |
none |
If the first character is not one of these,
'@'
is assumed.
注意
The number 1023 (
0x3ff
in hexadecimal) has the following byte representations:
-
03 ffin big-endian (>) -
ff 03in little-endian (<)
Python example:
>>> import struct
>>> struct.pack('>h', 1023)
b'\x03\xff'
>>> struct.pack('<h', 1023)
b'\xff\x03'