1

I need to loop through an array of byte arrays and then select a matching element from a dictionary. However my attempt to join the byte array fails:

roms = {
  "\xff\xfe\x88\x84\x16\x03\xd1":"living_room",
  "\x10\xe5x\xd5\x01\x08\x007":"bed_room"
}

devices = [bytearray(b'(\xff\xfe\x88\x84\x16\x03\xd1'), bytearray(b'\x10\xe5x\xd5\x01\x08\x007')]
for device in devices:
  DEV = "".join(device)
  print(roms[DEV])

>> TypeError: sequence item 0: expected str instance, int found

So it appears that you can't join an integer, is there another way?

UPDATE 1

With much help and patience from @falsetrue, I have managed to join the array. However the resulting string still throws a key error when I attempt to get the devices dictionary item:

roms = {
  "\xff\xfe\x88\x84\x16\x03\xd1":"living_room",
  "\x10\xe5x\xd5\x01\x08\x007":"bed_room"
}

devices = [bytearray(b'(\xff\xfe\x88\x84\x16\x03\xd1'), bytearray(b'\x10\xe5x\xd5\x01\x08\x007')]

for device in devices:
  DEV = str(bytes(device)).strip('b').strip("'").strip('(') # > this results in: \xff\xfe\x88\x84\x16\x03\xd1 - but still gives keyError
  #DEV = bytes(device).lstrip(b'(') # > This results in: b'\xff\xfe\x88\x84\x16\x03\xd1' - keyError
  print(DEV)
  print(roms["\xff\xfe\x88\x84\x16\x03\xd1"])
  print(roms[DEV])
  print()

>> \xff\xfe\x88\x84\x16\x03\xd1
>> living_room
>> KeyError: \xff\xfe\x88\x84\x16\x03\xd1

UPDATE 2

Here's the device info:

release='1.3.0.b1', 
version='v1.8.6-379-gc44ebac on 2017-01-13', 
machine='WiPy with ESP32'

Maybe someone else with a WIPY2 can verify this for me?

7
  • Sounds like an X-Y problem. There is no reason to "join" a byte array as you've shown it. You're not even joining two together. Please explain more. Also, shouldn't your keys in roms be bytes strings? For example: b'\xff\xfe...'. If they were, and devices were also byte strings, then roms[devce] would just work. Commented Jan 17, 2017 at 3:07
  • I didn't suggest to use str; it will add b'...' and escape bytes; It will make situtation worse; don't do that! Commented Jan 17, 2017 at 6:16
  • Just for reference, what version of Micropython is this - pyboard, WiPy, etc? Commented Jan 17, 2017 at 9:55
  • @nekomatic - 'WiPy', release='1.3.0.b1', version='v1.8.6-379-gc44ebac on 2017-01-13', machine='WiPy with ESP32' Commented Jan 17, 2017 at 9:59
  • OK, that's worth mentioning in any further questions. The original CC3200 based WiPy had a significantly cut-down version of MicroPython compared to the pyboard reference version, but it looks as if the ESP32 WiPy is closer to the reference. Commented Jan 17, 2017 at 10:11

1 Answer 1

1

If you're using Python 3.x:

You can decode the bytes to str using bytes.decode (or bytearray.decode)

devices = [bytearray(b'\xff\xfe\x88\x84\x16\x03\xd1'),
           bytearray(b'\x10\xe5x\xd5\x01\x08\x007')]
for device in devices:
    DEV = device.decode('latin1')  # Use bytes.decode to convert to str
                                   # (or bytearray.decode)
    print(roms[DEV])

prints

living_room
bed_room

BTW, I removed ( in byte literal.

devices = [bytearray(b'(\xff\xfe\x88\x84\x16\x03\xd1'), ...
                       ^

UPDATE

If you're using Python 2.x:

Convert the device to bytes using bytes function:

for device in devices:
    DEV = bytes(device)
    print(roms[DEV])
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks so much for helping, but when I run that I get: "AttributeError: 'bytearray' object has no attribute 'decode'"
@crankshaft, How about DEV = bytes(device).decode('latin1') ?
Made some progress, that line does not error, but when I try and use the DEV to get the dictionary device I get another error: "KeyError: (�����"
@crankshaft, I misunderstood you are using Python 3.x. I updated the answer, please check it out. In short, try DEV = bytes(device). micropython seems not fully implemented Python 3.x.
@crankshaft, As I wrote in the answer, there is a extra ( in the bytearray literal. Was it intentional?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.