why does this happen in python3?
1) I get msgpack data from redis
2) I unpack I get the below
3) The returned type is a dict:
meta = msgpack.unpackb(data[1])
print(type(meta))
<class 'dict'>
meta = {b'api_key': b'apikey1',
b'sensor_id': b'sid1',
b'version': b'1.0'}
If I run the below: sensor_meta['sensor_id']
{b'api_key': b'apikey1',
b'sensor_id': b'sid1',
b'version': b'1.0'}
Traceback (most recent call last):
File "/Users//worker.py", line 247, in <module>
print(meta['sensor_id'])
KeyError: 'sensor_id'
but if I use sensor_meta[b'sensor_id'] then it works.
What is the "b" and how can i get rid of that? How do I convert the whole object so there are no b's ?
so if I do the below:
print(type(meta['sensor_id']))
<class 'bytes'>
why bytes and how did it get there? I do not to append a b for every time I want to use keys in a hash.
Thanks
As mentioned in the notes here:
You can define an encoding while unpacking to convert your bytes to strings.