Hi following last week's discussion with Bob Gailer about reading unformatted FORTRAN files, I have attached an example of the file in ASCII format and the equivalent unformatted version. Below is some code that works OK until it gets to a data item that has no additional associated data, then seems to have got 4 bytes ahead of itself. I though I had trapped this but it appears not. I think the issue is asociated with "newline" characters or the unformatted equivalent.
Thanks in advance Alun Griffiths ============================== # 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
'IFLAGS ' 12 'INTE' -1 2 1 10500 3 10500 2 -1 100 1 105 10500 'QFLAGS ' 10 'LOGI' F T F F F T F F F F 'NAMES ' 3 'CHAR' 'ORTHO ' 'META ' 'PARA ' 'DATABEGI' 0 'MESS' 'TIME ' 1 'REAL' 0.00 'DISTANCE' 10 'DOUB' 0.00000000000000D+00 1.00000000000000D+00 2.00000000000000D+00 3.00000000000000D+00 4.00000000000000D+00 5.00000000000000D+00 6.00000000000000D+00 7.00000000000000D+00 8.00000000000000D+00 9.00000000000000D+00 'CONC ' 10 'REAL' 0.10495859E+02 0.10480556E+02 0.10465680E+02 0.10451221E+02 0.10437166E+02 0.10423511E+02 0.10410242E+02 0.10397354E+02 0.10384837E+02 0.10372684E+02 'DATAEND ' 0 'MESS'
test.bin
Description: Binary data
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor