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

これの何が問題なのですか?助けてください。

4

1 に答える 1

6

Python 3では、 Unicodeではなく、値struct.unpack()などのバッファプロトコルを実装するオブジェクトが必要です。bytesstr

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

このデータをファイルから読み取る場合は、テキストモードではなくバイナリモードでファイルを開いてバイトを取得します。

于 2013-02-19T17:30:19.280 に答える