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

