Python - Printing/Reading first Element 通常是 bytes 对象的任何元素

Python - Printing/Reading first Element generally any element of bytes object

如何 print/reading bytes 对象的第一个元素或通常的任何元素?

a = b'BMv\x01\x00\x00\x00\x00'

我知道第一个元素是b'B'。我可以用 a[0] 读取它,但它 returns 我的 INT 值 66。我知道这代表 b'B' 因为我从 int.from_bytes(b'B', byteorder='big') 得到值 66。我能做什么做得到 b'B' 而不是 INT 值 66?

一般来说,切片是父类型的实例。 a[:1] 是一个 bytes 长度为 1 的对象:

In [6]: a[:1]
Out[6]: b'B'

也可以转换整数列表(所有整数必须在[ 0, 255 ]) 到 bytes 对象,方法是将该列表传递给 bytes 构造函数:

In [7]: bytes([a[0]])
Out[7]: b'B'