如何在python中将字节字符串拆分为单独的字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20024490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split a byte string into separate bytes in python
提问by Razzad777
Ok so I've been using python to try create a waveform image and I'm getting the raw data from the .wavfile using song = wave.open()and song.readframes(1), which returns :
好的,所以我一直在使用 python 来尝试创建一个波形图像,我正在.wav使用song = wave.open()and从文件中获取原始数据song.readframes(1),它返回:
b'\x00\x00\x00\x00\x00\x00'
What I want to know is how I split this into three separate bytes, e.g. b'\x00\x00', b'\x00\x00', b'\x00\x00'because each frame is 3 bytes wide so I need the value of each individual byte to be able to make a wave form. I believe that's how I need to do it anyway.
我想知道的是我如何将其拆分为三个单独的字节,例如b'\x00\x00', b'\x00\x00',b'\x00\x00'因为每帧是 3 个字节宽,所以我需要每个单独字节的值才能形成波形。我相信无论如何我都需要这样做。
采纳答案by Martijn Pieters
You can use slicing on byteobjects:
您可以对byte对象使用切片:
>>> value = b'\x00\x01\x00\x02\x00\x03'
>>> value[:2]
b'\x00\x01'
>>> value[2:4]
b'\x00\x02'
>>> value[-2:]
b'\x00\x03'
When handling these frames, however, you probably also want to know about memoryview()objects; these let you interpret the bytes as C datatypes without any extra work on your part, simply by casting a 'view' on the underlying bytes:
但是,在处理这些帧时,您可能还想了解memoryview()对象;这些使您可以将字节解释为 C 数据类型,而无需您做任何额外的工作,只需在底层字节上投射“视图”即可:
>>> mv = memoryview(value).cast('H')
>>> mv[0], mv[1], mv[2]
256, 512, 768
The mvobject is now a memory view interpreting every 2 bytes as an unsigned short; so it now has length 3 and each index is an integer value, based on the underlying bytes.
该mv对象现在是一个内存视图,将每 2 个字节解释为一个无符号短整型;所以它现在的长度为 3,每个索引都是一个整数值,基于底层字节。
回答by kyle k
Here is a way that you can split the bytes into a list:
这是一种将字节拆分为列表的方法:
data = b'\x00\x00\x00\x00\x00\x00'
info = [data[i:i+2] for i in range(0, len(data), 2)]
print info
gives the result:
给出结果:
['\x00\x00', '\x00\x00', '\x00\x00']
回答by AndrewStone
You are actually asking about serialization/deserialization. Use struct.pack and struct.unpack (https://docs.python.org/3/library/struct.html). This gives you nice primitives to do both unpacking and things like endian swapping. For example:
您实际上是在询问序列化/反序列化。使用 struct.pack 和 struct.unpack ( https://docs.python.org/3/library/struct.html)。这为您提供了很好的原语来执行解包和字节序交换等操作。例如:
import struct
struct.unpack("<H",b"\x00\x01") # unpacks 2 byte little endian unsigned int
struct.unpack(">l",b"\x00\x01\x02\x03") # unpacks 4 byte big endian signed int
Note that your example splits 2 byte words, not bytes.
请注意,您的示例拆分了 2 个字节的字,而不是字节。
Since this question is also coming up in searches about splitting binary strings:
由于这个问题也在关于拆分二进制字符串的搜索中出现:
value = b'\x00\x01\x00\x02\x00\x03'
split = [value[i] for i in range (0, len(value))]
# now you can modify, for example:
split[1] = 5
# put it back together
joined = bytes(split)

