Hello Dan, > I am trying to head in a binary file that has a header and different > character types. The array module apparently expects the typecode to > be the same throughout. Here's what the file looks like: > > Byte number: type: value: purpose: > 1-4 char "ver." > 5-8 char "0001" version number > 9-12 int 1, 2, 3, or 4 data type: 1=unsigned byte > 2=unsigned short 3=32-bit integer 4=32-bit floating point > > 13-16 int x dimension, xdim > 7-20 int y dimension, ydim > 20-24 int z dimension, zdim > 25-28 int xdim*ydim*zdim > > The rest of the file contains the data array of type in indicated in > bytes 9-12. > > I have tried: > import Numeric as N > import array > fileobj = open(myfile, mode='rb') > s = array.array('f') > s.read(fileobj, size) > data = N.array(s, typecode=N.Int) > fileobj.close() > > But the header must confuse things because I do get what I expect. I'd use the struct module to read the header and then create an array according to type. Note: The code below is untested and without error checking.
from struct import struct from array import array from os.path import getsize TYPES = { # Type of array 1 : "c", 2 : "H", 3 : "i", 4 : "f" } filesize = getsize(filename) # File size fo = open(filename, "rb") header = fo.read(12) # Read interesting header information fields = unpack("ssssssssi", header) fo.read(15) # Skip non-data header a = array(TYPES[fields[-1]) # Create array size = (filesize - 28) / a.itemsize # Size to read a.read(fo, size) # Read to array Also check that you don't have little/big endien problems. HTH. -- ------------------------------------------------------------------------ Miki Tebeka <[EMAIL PROTECTED]> http://tebeka.bizhat.com The only difference between children and adults is the price of the toys
pgpKJa01Vry5h.pgp
Description: PGP signature
-- http://mail.python.org/mailman/listinfo/python-list