Skip to main content

utf8dict

Recursively iterates through a dictionary and decodes any keys that are byte strings into the specified encoding.

def utf8dict(
d: dict,
encoding: string = 'utf-8'
) - > dict

Recursively decodes byte-string keys in a dictionary into strings using the specified encoding. Use this to normalize dictionary keys when processing data from sources that return raw bytes instead of text.

Parameters

NameTypeDescription
ddictThe source dictionary containing keys that may need decoding from bytes to strings.
encodingstring = 'utf-8'The character encoding used to decode byte-string keys, defaulting to UTF-8.

Returns

TypeDescription
dictA new dictionary where all byte-string keys have been decoded into strings, while non-byte keys and all values remain unchanged.