开发者

python compare each 2bytes in some portion of data

In python, assume that there is data when I run...

search = target.readframes(2205)

Each frame consist of 2 bytes. I want to compare each 2-byte value and extract highest 2 values in the range.

For example, if the data looks like this...

0000|0001|0002|0008|0007|000F|000D|000A|00FB|00FC|00FA|00F9|00F8|00D7|00C3|0000

Then the result woul开发者_JS百科d extract 000F and 00FC

Could someone please help me achieve this. Any answers or helpful advise would be great.


First, you should use only bytes objects if you deal with binary data. They require Python 2.6+.

Example

data = b"\x42\x43\x44\x45"
print(data[0:2], data[2:4])

And yes, you can use normal compare operations with bytes.

I am not sure that kind of object is returned by your readframes but if it is not bytes by design you should convert it to bytes. You can just use data = bytes(obj).

Do not use strings to process binary data.


I understand you handle audio data (assuming target is a wave.Wave_read object), and want to find the maximum value for each channel.

import audioop

channel_l= audioop.tomono(search, 2, 1.0, 0.0)
channel_r= audioop.tomono(search, 2, 0.0, 1.0)
max_l= audioop.max(channel_l, 2)
max_r= audioop.max(channel_r, 2)

If yes, you should be more explicit in your questions; if not, feel free to downvote as not useful, people :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜