How do I fix this error? TypeError: 'str' does not support the buffer interface

依然范特西╮ 提交于 2019-12-11 05:55:33

问题


>>> 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!