[Tutor] Reading binary files #2

Alan Gauld alan.gauld at btinternet.com
Mon Feb 9 18:11:55 CET 2009


<etrade.griffiths at dsl.pipex.com> wrote

> I have attached an example of the file in ASCII format and the
> equivalent unformatted version.

Comparing them in vim...
It doesn't look too bad except for the DATABEGI / DATAEND message 
format.
That could be tricky to unravel but we have no clear format for MESS.
But I assume that all the stuff between BEG and END is supposed to
be effectively nested?.

> it gets to a data item that has no additional associated data,
> then seems to have got 4 bytes ahead of itself.

You are creating a format string of >0d but I'm not sure how struct
behaves with zero lenths...

HTH,

Alan G.

==============================
# Test function to write/read from unformatted files

import sys
import struct

# Read file in one go

in_file = open("test.bin","rb")
data = in_file.read()
in_file.close()

# Initialise

nrec = len(data)
stop = 0
items = []

# Read data until EOF encountered

while stop < nrec:

    # extract data structure

    start, stop = stop, stop + struct.calcsize('4s8si4s8s')
    vals = struct.unpack('>4s8si4s8s', data[start:stop])
    items.extend(vals)
    print stop, vals

    # define format of subsequent data

    nval = int(vals[2])

    if vals[3] == 'INTE':
        fmt_string = '>i'
    elif vals[3] == 'CHAR':
        fmt_string = '>8s'
    elif vals[3] == 'LOGI':
        fmt_string = '>i'
    elif vals[3] == 'REAL':
        fmt_string = '>f'
    elif vals[3] == 'DOUB':
        fmt_string = '>d'
    elif vals[3] == 'MESS':
        fmt_string = '>%dd' % nval
    else:
        print "Unknown data type ... exiting"
        print items
        sys.exit(0)

    # extract data

    for i in range(0,nval):
        start, stop = stop, stop + struct.calcsize(fmt_string)
        vals = struct.unpack(fmt_string, data[start:stop])
        items.extend(vals)

    # trailing spaces

    if nval > 0:
        start, stop = stop, stop + struct.calcsize('4s')
        vals = struct.unpack('4s', data[start:stop])

# All data read so print items

print items


-------------------------------------------------
Visit Pipex Business: The homepage for UK Small Businesses

Go to http://www.pipex.co.uk/business-services




--------------------------------------------------------------------------------


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




More information about the Tutor mailing list