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