Skip to main content
 首页 » 编程设计

python之我该如何解决这个错误类型错误 : 'str' does not support the buffer interface

2025年05月04日29bonelee

>>> import struct 
>>> s = '\x00\x00\x00\x01\x00\x00\x00\xff\xff\x00\x00' 
>>> struct.unpack('11B', s) 
Traceback (most recent call last): 
  File "<pyshell#3>", line 1, in <module> 
    struct.unpack('11B', s) 
TypeError: 'str' does not support the buffer interface 

这有什么问题吗?请帮忙。

请您参考如下方法:

在 python 3 上,struct.unpack() 需要一个实现缓冲协议(protocol)的对象,例如 bytes值,而不是 unicode str :

>>> import struct 
>>> s = b'\x00\x00\x00\x01\x00\x00\x00\xff\xff\x00\x00' 
>>> struct.unpack('11B', s) 
(0, 0, 0, 1, 0, 0, 0, 255, 255, 0, 0) 

如果您从文件中读取此数据,请以二进制模式而不是文本模式打开文件以获取字节。