liveBook · Manning

Struct module

As explained in The Quick Python Book, Fourth Edition

The struct module is a Python standard-library module for converting between Python values and packed binary representations.

The struct module converts between Python values and packed binary representations. It is especially useful when reading or writing binary files generated by, or intended for, C programs. section 13.0 section 13.7

A format string describes the layout of one binary record:

  • Use h for a C short integer, d for a C double-precision float, and s for a string. section 13.7
  • Open the file in binary mode and use bytes rather than strings for binary input and output. section 13.7

Packing and reading fixed records

A record format of 'hd7s' stores a short integer, a double, and seven bytes. Packing (42, 3.14, b'goodbye') produces one binary record. Reading repeatedly in record_size-byte chunks, stopping on an empty result, and unpacking each chunk reconstructs [(42, 3.14, b'goodbye')].

For simple storage, text or bytes input and output is usually preferable; for Python-object persistence, pickle is less error-prone than defining a binary layout manually. section 13.7

import struct
record_format = 'hd7s'
data_record = struct.pack(record_format, 42, 3.14, b'goodbye')
data_file = open("data", "wb")
data_file.write(data_record)
data_record
 
b'*\x00\x00\x00\x00\x00\x00\x00\x1f\x85\xebQ\xb8\x1e\t@goodbye'

View the complete packing example, including the format string, packed bytes, and file write. section 13.7

import struct
 
record_format = 'hd7s'
record_size = struct.calcsize(record_format)
result_list = []
with open("data", 'rb') as input: 
    while True:
        record = input.read(record_size)                         #1
        if not record:                                           #2
            break
        result_list.append(struct.unpack(record_format, record)) #3
 
print(result_list)
 
[(42, 3.14, b'goodbye')]]
#1 Reads in a single record
#2 Checks to see if record is empty
#3 Unpacks record into a tuple; appends to results

View the complete reading loop, including record sizing, end-of-file detection, and tuple reconstruction. section 13.7

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains format strings, packing, sizing, unpacking, and a record-reading loop, without detailing every format option.
Examples
Demonstrates an h-d-7s record containing 42, 3.14, and b'goodbye', then reads repeated fixed-size records.
What it assumes
Assumes familiarity with file objects and binary mode while teaching format strings through a concrete record layout.
What you can do after
Apply it via Python's struct module to pack and unpack fixed-format binary records.
How it connects
Connects binary files, bytes, file objects, C programs, and pickle.

Also explained in

Compare how 2 books explain struct module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.