Reading binary file in Python
I'm trying to read in the header from a binary file format called XTF. The specification is in this document (pdf) http://www.tritonimaginginc.com/site...Format_X18.pdf.
Since this is my first attempt at reading binary files, and since my python is pretty poor, I'm finding it difficult interpreting the information in the spec. For example, the spec says: Quote:
Code:
#!/usr/bin/env python Code:
{MaxView223$D:\lcusbl\calibration1309.xtfPort CM2 S/S�@Stbd CM2 S/S�@ Quote:
How do I go about correctly decoding these to get the information I need? Any pointers greatly appreciated. |
Quote:
Anyway, your problem is not reading binary file in Python, but extracting fields from the header you've read. The table clearly states field offsets and field lengths, so you need to extract block of characters of given offsets and given lengths from 's' variable. I am not a Python guy; in Perl there is 'substr' function which could easily be used to do the task, I guess in Python there are string manipulation functions doing the same. ... I've tried python string manipulation python substring - the latter in Yahoo produces this very first match: http://www.tutorialspoint.com/python/python_strings.htm which, I think, is what you need to extract fields. |
Quote:
Quote:
|
Take a look at the struct module.
|
Quote:
It is, however, laborious work because the header's 1024 bytes long, and you can fit a lot on information in 1024 bytes, it seems! Here's a sample, in case anyone ever needs an idea of what I'm talking about: Code:
PacketHeader=f.read(256) |
All times are GMT -5. The time now is 05:36 AM. |