<div>Hello everyone,</div>
<div> </div>
<div>Here's my solution for reading binary data (unformatted mixed types) and packing it into a dictionary. It works, but somehow doesn't seem so 'pythonic'. Just seeking comments on how I might make it more efficient. Thanks!
</div>
<div> </div>
<div><font face="courier new,monospace">def readheader(filename):<br> import struct<br> I={0:'rl',1:'ibdate',2:'ibtime',3:'version',4:'void1',5:'void2',6:'loutstep',7:'loutaver',8:'loutsample',9:'void3',10:'void4',11:'outlon0',12:'oulat0',13:'numxgrid',14:'numygrid',15:'dxout',16:'dyout',17:'void5',18:'void6',19:'numzgrid'}
<br> B={}<br> f2=file(filename,'rb')<br> #Define Header format<br> Dfmt=['i','i','i','13s','i','i','i','i','i','i','i','f','f','i','i','f','f','i','i','i'] #format for binary reading first bits
<br> if f2:<br> print filename + ' has been opened'<br> #create a dictionary from header file<br> a=[struct.unpack(ft,f2.read(struct.calcsize(ft))) for ft in Dfmt]<br> for i in range(len(a)):<br> B[I[i]]=a[i][0]
</font></div>