【问题标题】:python 'NoneType' object has no attribute 'from_bytes' [closed]python'NoneType'对象没有属性'from_bytes'[关闭]
【发布时间】:2015-01-29 08:44:05
【问题描述】:

请帮忙,这让我发疯了。

def get_pinstatus(*args):
    ser.write("p".encode())
    time.sleep(1) # wait for buffer to fill up
#   for i in range (16):
    value= ser.read(1)
    new_value = int.from_bytes(value, byteorder='big')

这是相当简单的代码,它将从串行读取 16 个单独的字节,我需要将每个字节转换为 8 位 int。如果我在 python 命令行中键入最后 4 条语句,它会完美运行,只是在函数中时不行。 无论我如何格式化代码,我总是收到以下错误:

new_value = int.from_bytes(value, byteorder='big')

AtrributeError: 'NoneType' object has no attribute 'from_bytes'

【问题讨论】:

  • 或许可以考虑struct.pack

标签: python attributeerror nonetype


【解决方案1】:

您在某处将int 重新定义为无。

【讨论】:

    【解决方案2】:

    除了 Daniel 提到的 - 这是你的错误的原因,你最好使用 struct 模块,而不是:

    value= ser.read(1)
    new_value = int.from_bytes(value, byteorder='big')
    

    你可以使用:

    import struct
    
    int_values = struct.unpack('>16B', ser.read(16))
    

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2021-08-10
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      相关资源
      最近更新 更多