带有TypeError的Python struct.unpack错误:需要一个类似字节的对象,而不是'str' [英] Python struct.unpack errors with TypeError: a bytes-like object is required, not 'str'
问题描述
有人可以帮助下面的代码行和错误吗?我不熟悉python的值转换.
产生错误的特定行是:
value = struct.unpack("<h",chr(b)+chr(a))[0]
TypeError:需要一个类似字节的对象,而不是'str'
代码片段是:
if packet_code ==0x80: # raw value
row_length = yield
a = yield
b = yield
value = struct.unpack("<h",chr(b)+chr(a))[0]
输入数据为:
b'\ x04 \ x80 \ x02 \ x00 \ xb2 \ xcb \ xaa \ xaa \ x04 \ x80 \ x02 \ x00p \ r \ xaa \ xaa \ x04 \ x80 \ x02 \ x00] \ xaa \ xaa \ x04 \ x80 \ x02 \ x00 @ = \ xaa \ xaa \ x04 \ x80 \ x02 \ x007F \ xaa \ xaa \ x04 \ x80 \ x02 \ x00 \!\ xaa \ xaa \ x04 \ x80 \ x02 \ x00 = @ \ xaa \ xaa \ x04 \ x80 \ x02 \ x00 = @ \ xaa \ xaa \ x04 \ x80 \ x02 \ x00i \ x14 \ xaa \ xaa \ x04 \ x80 \ x02 \ x00] \ xaa \ xaa \ x04 \ x80 \ x02 \ x00p \ r \ xaa \ xaa \ x04 \ x80 \ x02 \ x00 \ x80 \ xfd \ xaa \ xaa
我正在使用python 3.5.该代码似乎可以在旧版本中使用.
以下是指向类似解析器代码的链接,该链接可能与以前的Python版本一起使用: 解析器代码链接 >
此处是有关如何数据是从设备发送的 RAW Wave值(16位)
此数据值由两个字节组成,代表一个原始波样本.它的值是一个有符号的16位整数,范围从-32768到32767.Value的第一个字节表示二进制补全值的高阶位,而第二个字节表示低序位.要重构完整的原始波值,只需将第一个字节左移8位,然后按位或与第二个字节左移:
short raw = (Value[0]<<8) | Value[2];
其中Value [0]是高位字节,而Value 解决方案
在使用Python 2.x时,str
是字节数组.对于Python 3,您必须像这样使用bytes
:
struct.unpack("<h", bytes([b, a]))[0]
Can someone please help with the following line of code and Error? I am unfamiliar with python value conversions.
The specific line that generates the error is:
value = struct.unpack("<h",chr(b)+chr(a))[0]
TypeError: a bytes-like object is required, not 'str'
The code fragment is:
if packet_code ==0x80: # raw value
row_length = yield
a = yield
b = yield
value = struct.unpack("<h",chr(b)+chr(a))[0]
The input data is:
b'\x04\x80\x02\x00\xb2\xcb\xaa\xaa\x04\x80\x02\x00p\r\xaa\xaa\x04\x80\x02\x00] \xaa\xaa\x04\x80\x02\x00@=\xaa\xaa\x04\x80\x02\x007F\xaa\xaa\x04\x80\x02\x00\!\xaa\xaa\x04\x80\x02\x00=@\xaa\xaa\x04\x80\x02\x00=@\xaa\xaa\x04\x80\x02\x00i\x14\xaa\xaa\x04\x80\x02\x00] \xaa\xaa\x04\x80\x02\x00p\r\xaa\xaa\x04\x80\x02\x00\x80\xfd\xaa\xaa
I am using python 3.5. This code seems to work in the older versions.
Here is the link to similar parser code where it may have worked with previous versions of Python: Parser Code Link
Here is the link to the description of how the data is sent from the device RAW Wave Value (16-bit)
This Data Value consists of two bytes, and represents a single raw wave sample. Its value is a signed 16-bit integer that ranges from -32768 to 32767. The first byte of the Value represents the high-order bits of the twos-compliment value, while the second byte represents the low-order bits. To reconstruct the full raw wave value, simply shift the first byte left by 8 bits, and bitwise-or with the second byte:
short raw = (Value[0]<<8) | Value[2];
where Value[0] is the high-order byte, and Value1 is the low-order byte.
In systems or languages where bit operations are inconvenient, the following arithmetic operations may be substituted instead:
raw = Value[0]*256 + Value[1];
if( raw >= 32768 ) raw = raw - 65536;
Really appreciate any help as I am currently stuck.
When you are using Python 2.x str
is a byte array. For Python 3, you must use bytes
like this:
struct.unpack("<h", bytes([b, a]))[0]
这篇关于带有TypeError的Python struct.unpack错误:需要一个类似字节的对象,而不是'str'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!