Decode it.
>>> b'a string'.decode('ascii')
'a string'
To get bytes from string, encode it.
>>> 'a string'.encode('ascii')
b'a string'
Answer from falsetru on Stack Overflow
ยป pip install binary-parser
Take a look at the struct module:
In [1]: import struct
In [2]: magic = b'\xcf\xfa\xed\xfe'
In [3]: decoded = struct.unpack('<I', magic)[0]
In [4]: hex(decoded)
Out[4]: '0xfeedfacf'
There's Kaitai Struct project that solves exactly that problem. First, you describe a certain file format using a .ksy spec, then you compile it into a Python library (or, actually, a library in any other major programming language), import it, and, voila, parsing boils down to:
from mach_o import MachO
my_file = MachO.from_file("/path/to/your/file")
my_file.magic # => 0xfeedface
my_file.num_of_sections # => some other integer
my_file.sections # => list of objects that represent sections
They have a growing repository of file format specs. It doesn't have Mach-O file format spec (yet?), but there are complex formats like Java .class or Microsoft's PE executable described there, so I guess it shouldn't be a major problem to write spec for Mach-O format as well.
It is actually better than Construct or Hachoir, because it's compiled (as opposed to interpreted), thus it's faster, and it includes tons of other helpful tools like visualizer or format diagram maker. For example, this is a generated explanation diagram for PE executable format:

You need to open your file in binary mode and read only 12 bytes from your file:
import struct
with open('abc.dat', 'rb') as fobj:
byte_string, n1, n4 = struct.unpack('4sbI', fobj.read(12))
You will get a byte string. Assuming it is ASCII, you can decode like this:
my_string = byte_string.decode('ascii')
The documentation of the struct contains tables of format strings.
According to one of these tables a uint64 would be L.
I believe you are trying to extract information from the binary. Well this will work
import struct
import numpy as np
buffer = np.random.bytes(12)
s = struct.Struct('4sbI')
unpacked_data = s.unpack(buffer)
print unpacked_data[0], unpacked_data[1], unpacked_data[2]
In this case unpacked_data[0] will be the string, unpacked_data[1] will be the 1 byte number and the 4 byte integer will be unpacked_data[2].
Keep in mind you can also use numpy to unpack the data using the np.ndarray constructor if you would like to improve the speed.