Python struct Module
Example
Pack integers into bytes and unpack them:
import struct
packed = struct.pack('ii', 42, 100)
print(f'Packed bytes: {packed}')
unpacked = struct.unpack('ii', packed)
print(f'Unpacked values: {unpacked}')
Try it Yourself »
Definition and Usage
The struct module converts between Python values and C structs represented as Python bytes objects.
Use it to work with binary data from files or networks, or to interface with C libraries that use packed binary data.
Members
Member | Description |
---|---|
Struct | Class for compiled struct format. |
calcsize() | Return size in bytes of struct format string. |
error | Exception raised on struct-related errors. |
iter_unpack() | Iteratively unpack buffer according to format. |
pack() | Pack values into bytes according to format string. |
pack_into() | Pack values into buffer at given offset. |
unpack() | Unpack bytes according to format string. |
unpack_from() | Unpack buffer from given offset according to format. |