reading binary data in python

Reading Binary Data in Python

The issue of reading Binary data in Python is not difficult - but it requires some care.

Writing Binary Data

Before we can read data - we should first write some data.

I am being very specific about the fields I am writing

Field Size
Byte 1
Byte 1
Byte 1
CHAR 20
CHAR 20
Byte 1
Byte 1
Byte 1

Unless you are told what the Format of the file is - reading the data back - will be very very difficult indeed.

Python 2.7

import struct
from random import Random 
''' Generate a Data file 
    byte byte byte 20char 20char byte byte byte
'''
r=Random()
bfile=open('data.bin','wb')
for i in range(100):
    x=i%255
    y=x+1%255
    z=y+1%255
    tx=(str(99340000+r.randint(1,99999))+' '*20)[0:20]
    rx=(str(99340000+r.randint(1,99999))+' '*20)[0:20]
    print("%d %d %d %s %s %d %d %d"%(x,y,z,tx,rx,x,y,z))
    bfile.write(struct.pack('b b b 20s 20s b b b',x,y,z,tx,rx,x,y,z))
    bfile.write('\n')
bfile.close()

Python 3.x

import struct
from random import Random
''' Generate a Data file
    byte byte byte 20char 20char byte byte byte
'''
r=Random()
bfile=open('data.bin','wb')
for i in range(100):
    x=i%255
    y=x+1%255
    z=y+1%255
    tx=(str(99340000+r.randint(1,99999))+' '*20)[0:20]
    rx=(str(99340000+r.randint(1,99999))+' '*20)[0:20]
    print(("%d %d %d %s %s %d %d %d"%(x,y,z,tx,rx,x,y,z)))
    bfile.write(struct.pack('b b b 20s 20s b b b',x,y,z,bytes(tx,'UTF-8'), bytes(rx,'UTF-8'),x,y,z))
    bfile.write(bytes('\n','UTF-8'))
bfile.close()

Reading

This works in 2.7 and 3.x versions of Python

import struct
import pprint

binin=open('data.bin','rb')
try:
    while True:
       line=binin.read(47)
       print("process")
       x,y,z,tx,rx,x,y,z = struct.unpack('b b b 20s 20s b b b x',line)
       ''' Or '''
       data=struct.unpack('b b b 20s 20s b b b x',line)
       fields=('x','y','z','tx','rx','x2','y2','z2')
       d=dict(zip(fields,data))
       pprint.pprint(d)
except:
    pass
binin.close()

Output

To Check what come out - this is what I see when I run the read program

process
{'rx': b'99398009            ',
 'tx': b'99423917            ',
 'x': 98,
 'x2': 98,
 'y': 99,
 'y2': 99,
 'z': 100,
 'z2': 100}
process
{'rx': b'99420550            ',
 'tx': b'99408317            ',
 'x': 99,
 'x2': 99,
 'y': 100,
 'y2': 100,
 'z': 101,
 'z2': 101}
process