How to deserialize the Transaction Count in a raw block? In the bitcoin-book, the &quot;Transaction Counter&quot; part of a block is documented as being 1~9 bytes long instead of a fixed size.
How is this counter parsed from the raw data?
This number is encoded as a variable-length integer, see the txn_count in the block protocol message.
The idea is the following:

Read the first byte as marker
If marker &lt; 0xFD

marker is the number


If marker == 0xFD

read the next 2 bytes as the number


If marker == 0xFE

read the next 4 bytes as the number


If marker == 0xFF

read the next 8 bytes as the number



Or, in Python:
def read_varint(byte_array):
    marker = byte_array[0]
    if marker &lt; 0xfd:
        return marker
    elif marker == 0xfd:
        return int.from_bytes(byte_array[1:3], 'little')
    elif marker == 0xfe:
        return int.from_bytes(byte_array[1:5], 'little')
    else:
        return int.from_bytes(byte_array[1:9], 'little')

See also the implementation in Bitcoin Core here.