I have some binary data that I want to read via Python. I have the C code to do this, but I want to try it in Python.
Let's say the header of the file is as follows:
typedef struct
{
uint64_t message_time;
float lon, lat, alt;
int32_t extraint32[16];
float extrafloat[16];
char aircraft_name[100];
} AIRCRAFT_HEADER;
Using Python, I do the following:
from ctypes import *
class aircraft_header(Structure):
_fields_ = [('message_time', c_uint64),
('lon', c_float), ('lat', c_float), ('alt', c_float),
('extraint32[16]', c_int32),
('extrafloat[16]', c_float),
('aircraft_name[100]', c_char)
]
with open("../data/somefile.bin",mode='rb') as file:
result = []
allc = aircraft_header()
while file.readinto(allc) == sizeof(allc):
result.append((allc.message_time,
allc.lon, allc.lat, allc.alt,
allc.extraint32,
allc.extrafloat,
allc.aircraft_name))
This will throw the exception:
AttributeError: 'aircraft_header' object has no attribute 'extraint32'
How do I resolve this? Also, is there a more Pythonic way of reading this binary file?