title: Reading a binary file into a struct
body: <p>I have a binary file with a known format/structure.</p>
  
  <p>How do I read all the binary data in to an array of the structure?</p>
  
  <p>Something like (in pseudo code)</p>
  
  <pre><code>bytes = read_file(filename)
  struct = {'int','int','float','byte[255]'}
  data = read_as_struct(bytes, struct)
  data[1]
  &gt;&gt;&gt; 10,11,10.1,Arr[255]
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Solution so far:</p>
  
  <pre><code>data = []
  
  fmt   = '=iiiii256i'
  fmt_s = '=iiiii'
  fmt_spec = '256i'
  
  struct_size = struct.calcsize(fmt)
  
  for i in range(struct_size, len(bytes)-struct_size, struct_size):
      dat1= list(struct.unpack(fmt_s, bytes[i-struct_size:i-1024]))
      dat2= list(struct.unpack(fmt_spec, bytes[i-1024:i]))
      dat1.append(dat2)
      data.append(dat1)
  </code></pre>