[python-win32] How to parse a binary file

Jorgensen, Jens jens.jorgensen@tallan.com
Thu, 05 Jul 2001 16:40:47 -0500


David Ransier wrote:

>I've never written a binary parser before, so I'm looking for some starting
>point.
>
>I want to read an image file (Sun Raster format: RAS) and parse the included
>color map, writing the color map as a text file (possible CSV format).
>
>I can open the file in "rb" mode and read a certain number of bytes. But
>when I try to do something with the value, Python complains that it's not in
>the correct format.
>
>Here is some sample code for reading the file (Magic is a 4-byte ID that
>identifies the file as RAS format.)
>
>
>
>
>import sys
>
>infile =	"TestImage.ras"
>try:
>	FH = open(infile, 'rb')
>except IOError, eStr:
>	print "ERROR: Cannot open ", infile, " for reading: ", eStr
>	sys.exit()
>
>	Magic = FH.read(4)
>	print "Magic:  %d" % (Magic)
>
>
>
>
>I tried coercing Magic to an int, but that didn't help.
>
Well, this certainly isn't Win32 specific but what the heck. When you 
read data from a file it is *always* a string. In order to take this 
binary data and turn it into other things (like strings) you'll want to 
use the struct module. In the above case you could write:

Magic = struct.unpack("i", FH.read(4))[0]

-- 
Jens B. Jorgensen
jens.jorgensen@tallan.com