问题
>>> 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.
回答1:
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.
来源:https://stackoverflow.com/questions/14963699/how-do-i-fix-this-error-typeerror-str-does-not-support-the-buffer-interface