简体   繁体   中英

filtering values from bytearray

I have a list of bytearrays, some of the items are:

bytearray(b'0.266 : 2\x00\xc9\n\xa0\x01\x00\x00p\x99\xcd\x88')
bytearray(b'0.264 : 3\x00\xc9\n\xa0\x01\x00\x000.12')
bytearray(b'0.263 : 4\x00\xdc\n\xa0\x01\x00\x000.25')
bytearray(b'0.255 : 5\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00')
bytearray(b'0.266 : 6\x00\xce\n\xa0\x01\x00\x000.13')
bytearray(b'0.263 : 7\x00\xd6\n\xa0\x01\x00\x00\xe0\x98]\n')
bytearray(b'0.273 : 8\x00\x00\x00\x00\x00\x00\x00\xa0\x9d]\n')
bytearray(b'0.269 : 9\x00\x00\x00\x00\x00\x00\x00\x10\x9d]\n')
bytearray(b'0.272 : 10\x00\x00\x00\x00\x00\x00\x10\x9f]\n')
bytearray(b'0.275 : 11\x00\x00\x00\x00\x00\x00@\x9d]\n')
bytearray(b'0.272 : 12\x00\x00\x00\x00\x00\x00\xd0\x9e]\n')
bytearray(b'0.274 : 13\x00\x00\x00\x00\x00\x000.26')
bytearray(b'0.278 : 14\x00\x00\x00\x00\x00\x00 \x9d]\n')
bytearray(b'0.272 : 15\x00\x00\x00\x00\x00\x000\x9f]\n')
bytearray(b'0.27 : 16\x00\x00\x00\x00\x00\x00\x00 \x9d]\n')
bytearray(b'0.276 : 17\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00')
bytearray(b'0.272 : 18\x00\x00\x00\x00\x00\x00`\x9f]\n')
bytearray(b'0.277 : 19\x00\x00\x00\x00\x00\x00Pz\xb1\n')

I want to filter out the starting values ie 0.277: 19 , ideally in the form of a tuple. I tried decoding usign b.decode() but receive the followin error:

'utf-8' codec can't decode byte 0xf0 in position 8: invalid continuation byte

an important thing to note is that sometimes the floats have 2 decimal places but at max they have 3. Is there a simple way to get that information out in the form of a tuple?

arrays = [
    bytearray(b'0.266 : 2\x00\xc9\n\xa0\x01\x00\x00p\x99\xcd\x88'),
    bytearray(b'0.264 : 3\x00\xc9\n\xa0\x01\x00\x000.12'),
    bytearray(b'0.263 : 4\x00\xdc\n\xa0\x01\x00\x000.25'),
    bytearray(b'0.255 : 5\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00'),
    bytearray(b'0.266 : 6\x00\xce\n\xa0\x01\x00\x000.13'),
    bytearray(b'0.263 : 7\x00\xd6\n\xa0\x01\x00\x00\xe0\x98]\n'),
    bytearray(b'0.273 : 8\x00\x00\x00\x00\x00\x00\x00\xa0\x9d]\n'),
    bytearray(b'0.269 : 9\x00\x00\x00\x00\x00\x00\x00\x10\x9d]\n'),
    bytearray(b'0.272 : 10\x00\x00\x00\x00\x00\x00\x10\x9f]\n'),
    bytearray(b'0.275 : 11\x00\x00\x00\x00\x00\x00@\x9d]\n'),
    bytearray(b'0.272 : 12\x00\x00\x00\x00\x00\x00\xd0\x9e]\n'),
    bytearray(b'0.274 : 13\x00\x00\x00\x00\x00\x000.26'),
    bytearray(b'0.278 : 14\x00\x00\x00\x00\x00\x00 \x9d]\n'),
    bytearray(b'0.272 : 15\x00\x00\x00\x00\x00\x000\x9f]\n'),
    bytearray(b'0.27 : 16\x00\x00\x00\x00\x00\x00\x00 \x9d]\n'),
    bytearray(b'0.276 : 17\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00'),
    bytearray(b'0.272 : 18\x00\x00\x00\x00\x00\x00`\x9f]\n'),
    bytearray(b'0.277 : 19\x00\x00\x00\x00\x00\x00Pz\xb1\n'),
]

print(len(arrays))  # 18

pattern_to_exclude = b'0.277 : 19'
filtered_arrays = list(filter(lambda bytes: bytes[0:len(pattern_to_exclude)] != pattern_to_exclude, arrays))

print(len(filtered_arrays))  # 17
print(filtered_arrays[-1][0:10])  # bytearray(b'0.272 : 18')

Just checking that the start of the bytearray does not match the pattern. I used filter but the same could be done with a regular loop.
The final print is there to show that the last bytearray is no longer present, as it was the only one matching the pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM