Example - I have a dict like this:
{b'mykey': {b'inner_key': b'inner_value'}}
How can I convert it to a dict where strings and values are all strings?
Example - I have a dict like this:
{b'mykey': {b'inner_key': b'inner_value'}}
How can I convert it to a dict where strings and values are all strings?
def decode_dict(d, encoding_used = 'utf-8'):
return { k.decode(encoding_used) : (v.decode(encoding_used) if isinstance(v, bytes) else decode_dict(v, encoding_used)) for k, v in d.items() }
new_dict = decode_dict({b'mykey': {b'inner_key': b'inner_value'}})
print(new_dict)
If your encoding is not UTF-8, you need to use the second argument in the call.
Use decode:
b'somestring'.decode('utf-8')
With a simple dictionary like {b'inner_key': b'inner_value'}
you could do something like this:
for k, v in list(d.items()):
d[k.decode('utf8')] = d.pop(k).decode('utf8')
This needs to be extended to more general cases in which there could be nested dictionaries. However it modifies the dictionary inplace, which could be useful if you do not want to create another dictionary.
I ended up doing
def _convert_bytes_dict(data):
out = {}
for key, val in data.items():
if isinstance(val, list):
decoded_val = [_convert_bytes_dict(i) for i in val]
elif not hasattr(val, "items") and not hasattr(val, "decode"):
decoded_val = val
elif not hasattr(val, "items"):
decoded_val = val.decode("utf-8")
else:
decoded_val = _convert_bytes_dict(val)
out[key.decode("utf-8")] = decoded_val
return out