我该如何解决这个错误?TypeError: 'str' 不支持缓冲区接口 [英] How do I fix this error? TypeError: 'str' does not support the buffer interface
本文介绍了我该如何解决这个错误?TypeError: 'str' 不支持缓冲区接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这有什么问题吗?请帮忙.
解决方案
在 python 3 上,struct.unpack()
需要一个实现缓冲协议的对象,例如 bytes
值,而不是 unicode str
:
如果您从文件中读取此数据,请以二进制模式而不是文本模式打开文件以获取字节.
>>> 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
What is wrong with this? Please help.
解决方案
On python 3, struct.unpack()
expects an object that implements the buffer protocol, such as a bytes
value, not a 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)
If you are reading this data from a file, open the file in binary mode instead of text mode to get bytes.
这篇关于我该如何解决这个错误?TypeError: 'str' 不支持缓冲区接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文