calcsize is a struct-module function that accepts a format string and returns the number of bytes required for data in that format. Use it to determine how many bytes to read for each binary record before calling struct.unpack. section 13.7
In a binary-record workflow, calcsize connects the format definition to file reading:
- Define one format string, such as 'hd7s', for the record layout. section 13.7
- Pass that string to struct.calcsize to obtain the record size. section 13.7
- Read that many bytes, then pass the record and the same format string to struct.unpack. section 13.7
Reading fixed-size records
With record_format = 'hd7s', set record_size = struct.calcsize(record_format), read record_size bytes in each loop iteration, and unpack each nonempty record with struct.unpack(record_format, record). 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
Shows calcsize used to control one-record-at-a-time reads and struct.unpack used to recover the original values. section 13.7
