python字节数组print()给出错误的值

时间:2018-12-18 21:42:39

标签: python arrays python-3.x format byte

使用python 3,我编写了以下脚本,该脚本由包含数百个字节数组的模型类组成,并且在模型类外部的同一脚本中,我将其中的一些打印出来以验证其正确性。当我打印值时,某些值不是我期望的。(我在下面的代码中添加了编码注释以标识这些值)

这是我的脚本的简化版本,其中包含一些字节数组

`
class Model:
    def __init__(self):

        # weird values:
        self.bp_diastole_118 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02\x3b')
        self.bp_diastole_120 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02\x3c')
        self.bp_diastole_122 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02\x3d')
        self.bp_diastole_124 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02\x3e')
        self.bp_diastole_126 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02\x3f')
        self.bp_diastole_128 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02\x40')
        self.bp_diastole_160 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02\x50')

        # correct values:
        self.pupil_r_normal = bytearray(b'\xff\x01\x02\x01\x01\x00\x03\xc3')
        self.pupil_r_dilated = bytearray(b'\xff\x01\x02\x01\x01\x00\x03\xc4')
        self.pupil_r_constriced = bytearray(b'\xff\x01\x02\x01\x01\x00\x03\xc5')
        self.pupil_r_reaction_on = bytearray(b'\xff\x01\x02\x01\x01\x00\x03\xc6')
        self.pupil_r_reaction_off = bytearray(b'\xff\x01\x02\x01\x01\x00\x03\xc7')




m = Model()  

print('--------------weird value------------------')
print('bp_diastole_118 = {}'.format(m.bp_diastole_118))
print('bp_diastole_120 = {}'.format(m.bp_diastole_120))
print('bp_diastole_122 = {}'.format(m.bp_diastole_122))
print('bp_diastole_124 = {}'.format(m.bp_diastole_124))
print('bp_diastole_126 = {}'.format(m.bp_diastole_126))
print('bp_diastole_128 = {}'.format(m.bp_diastole_128))
print('bp_diastole_160 = {}'.format(m.bp_diastole_160))

print('-------------correct value--------------------')
print('pupil_r_normal = {}'.format(m.pupil_r_normal))
print('pupil_r_dilated = {}'.format(m.pupil_r_dilated))
print('pupil_r_constriced = {}'.format(m.pupil_r_constriced))
print('pupil_r_reaction_on = {}'.format(m.pupil_r_reaction_on))
print('pupil_r_reaction_off = {}'.format(m.pupil_r_reaction_off))

这是打印到控制台的内容:

`
--------------weird value------------------
bp_diastole_118 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02;')
bp_diastole_120 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02<')
bp_diastole_122 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02=')
bp_diastole_124 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02>')
bp_diastole_126 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02?')
bp_diastole_128 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02@')
bp_diastole_160 = bytearray(b'\xff\x01\x02\x01\x01\x00\x02P')
-------------correct value--------------------
pupil_r_normal = bytearray(b'\xff\x01\x02\x01\x01\x00\x03\xc3')
pupil_r_dilated = bytearray(b'\xff\x01\x02\x01\x01\x00\x03\xc4')
pupil_r_constriced = bytearray(b'\xff\x01\x02\x01\x01\x00\x03\xc5')
pupil_r_reaction_on = bytearray(b'\xff\x01\x02\x01\x01\x00\x03\xc6')
pupil_r_reaction_off = bytearray(b'\xff\x01\x02\x01\x01\x00\x03\xc7')

如您所见,好的值完全打印了我期望的值,并且与我初始化的值相同。但是,如果您查看从奇怪的值打印的内容,您会看到最后3个字符与我初始化的值不匹配。


初始化:
self.bp_diastole_118 =字节数组(b'\ xff \ x01 \ x02 \ x01 \ x01 \ x00 \ x02 \ x3b')
与印刷品不同:
bp_diastole_118 =字节数组(b'\ xff \ x01 \ x02 \ x01 \ x01 \ x00 \ x02;')

有人知道为什么会这样吗,我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

我发现的问题是x3b到x50,它们似乎将最后一部分输出为特殊字符,x50为P。如果使用bytearray(b'\x3b')测试,它将为您显示结尾

答案 1 :(得分:0)

您正在看到所设置的十六进制值的utf表示形式,例如utf('0x3b')==';'