Binary file Pt 1 - Only reading some

Mastastealth mastastealth at gmail.com
Tue Feb 5 08:50:25 EST 2008


On Feb 5, 1:17 am, Gabriel Genellina <gagsl-... at yahoo.com.ar> wrote:
> 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

Ah ok, thanks! That worked, though the line "str1, str2, blank,
height, width, num2, num3 =" spit out a syntax error. However, I do
see that it creates a tuple with all the values in a readable format
for me. Also, I needed to change info.read(15) to 16. More questions:

What is this value for? "6s3s1cBBBh" and why is my unpack limited to a
length of "16"?

Unfortunately it seems my understanding of binary is way too basic for
what I'm dealing with. Can you point me to a simple guide to
explaining most of it? As far as I know this is just a bunch of 1's
and 0's right? Each byte has 8 digits of, of which somehow is
converted to a number or letter. Don't know what most of that stuff in
the struct page means. -_-

As for you questions, I suppose it would be "little-endian" as the
format is on PC (and the Python docs say: "Intel and DEC processors
are little-endian"). 0-5 means a single digit "0" through "5". Lastly,
I'm not building the format, it's already made (a format for tiled
maps in a game). My program is just reading it.



More information about the Python-list mailing list