Python - Search for hex value marker and extract data
Question: I am new to python and have tried searching for help prior to posting.

I have binary file that contains a number of values I need to parse. Each
value has a hex header of two bytes and a third byte that gives a size of the
data in that record to parse. The following is an example:

    
    
    \x76\x12\x0A\x08\x00\x00\x00\x00\x00\x00\x00\x00
    

The `\x76\x12` is the record marker and `\x0A` is the number of bytes to be
read next.

This data always has the two byte marker and a third byte size. However the
data to be parsed is variable and the record marker increments as follows:
`\x76\x12` and `\x77\x12` and so on until `\x79\x12` where is starts again.

This is just example data for the use of this posting.

Many Thanks for any help or pointers.


Answer: Is something like this what you want?

    
    
    >>> b = b'\x76\x12\x0A\x08\x00\x00\x00\x00\x00\x00\x00\x00'
    >>> from StringIO import StringIO
    >>> io = StringIO(b)
    >>> io.seek(0)
    >>> io.read(2) #read 2 bytes, maybe validate?
    'v\x12'
    >>> import struct
    >>> nbytes = struct.unpack('B',io.read(1))
    >>> print nbytes
    (10,)
    >>> data = io.read(nbytes[0])
    >>> data
    '\x08\x00\x00\x00\x00\x00\x00\x00\x00'
    

