Binary file Pt 1 - Only reading some

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 5 01:17:07 EST 2008


On 5 feb, 01:51, Mastastealth <mastastea... at gmail.com> wrote:
> I'm trying to create a program to read a certain binary format. I have
> the format's spec which goes something like:
>
> First 6 bytes: String
> Next 4 bytes: 3 digit number and a blank byte
> ---
> Next byte: Height (Number up to 255)
> Next byte: Width (Number up to 255)
> Next byte: Number 0 - 5
> Every 2 bytes after that: Supposedly a number 0000 - 0899?
>
> Anyway, I'm able to do the first 2 objects fine:
>
> a = info.read(6)
> b = info.read(4)
>
> Printing both gives me what I mentioned above, a string and a 3 digit
> number with a space. However, as I continue, things get trickier.
>
> c = info.read(1)
> d = info.read(1)
>
> Printing c and d in this case gives me a block in the SPE output, or
> if I run in a DOS prompt, 2 funny symbols. How do I get an integer out
> of this? I'll probably need help once I get to the "every 2 byte"
> section, but that'll be a separate post.

Using the struct module  http://docs.python.org/lib/module-struct.html

import struct
data = info.read(15)
str1, str2, blank, height, width, num2, num3 =
struct.unpack("6s3s1cBBBh", data)

Consider this like a "first attempt", open issues: is the data little-
endian or big-endian? does the 0-5 mean 0x00-0x05 or "0"-"5"? the last
numbers are 2-byte binary integers, or 0000-0899 might indicate BDC?
But building the right format is surely faster and easier than parsing
the data by hand.

--
Gabriel Genellina



More information about the Python-list mailing list