Master Python’s struct Module: Binary Data Packing & Unpacking Explained
This article introduces Python’s struct module, explaining how it converts between bytes and other binary data types, detailing its functions, format strings, alignment options, and practical examples of packing, unpacking, and size calculation for efficient binary data handling.
struct is a module that solves conversion between bytes and other binary data types, making stream operations simple and addressing Python's lack of a native byte data type.
Purpose
1. Convert Python data to a byte‑stream string for network transmission, e.g., converting int to bytes before sending.
2. Convert byte streams back to specified Python data types.
3. Handle binary data when reading/writing files in binary mode ('wb', 'rb').
Functions in the struct module
See the table below:
Format strings
When packing or unpacking, a format string specifies data types and controls byte order, size, and alignment.
Alignment
To exchange data with C structs, consider the compiler's byte alignment (commonly 4‑byte on 32‑bit systems). The first character of the format string can change alignment. Definitions:
Endianess: big‑endian stores the most significant byte at the highest address; little‑endian stores it at the lowest address.
Format specifiers
See the table below:
Notes:
_Bool is defined in C99; if unavailable, it is treated as a one‑byte char.
q and Q are for 64‑bit machines only.
A numeric prefix before a format indicates repetition (e.g., 4s for a 4‑character string, 4i for four ints).
P converts a pointer, size depends on the machine.
f and d sizes depend on the machine.
Examples
Typical packing converts any data type to bytes:
Unpacking bytes back to data types can cause overflow if the buffer length is incorrect; ensure the buffer length matches the expected size.
For image data, read the file first then unpack:
pack_into places multiple data objects into a buffer and converts them to a byte stream, allowing predefined packing ranges.
unpack_from converts a byte stream back to data objects, with optional offset.
calcsize computes the memory size of a format string:
Conclusion
This article introduced Python’s struct module for binary data handling, covering its purpose, functions, format strings, alignment, specifiers, and practical usage examples.
Reference:
https://blog.csdn.net/qq_30638831/article/details/80421019Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
