Created
August 9, 2019 12:54
-
-
Save ultrafunkamsterdam/971143ca69753ab2ab4057f67ef3ed21 to your computer and use it in GitHub Desktop.
stringify dict keys and values (nodes) recursively (where possible)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def stringify_nodes(data): | |
if isinstance(data, list): | |
return [stringify_nodes(x) for x in data] | |
elif isinstance(data, dict): | |
dkeys = list(data.keys()) | |
for i,k in enumerate(dkeys): | |
try: | |
dkeys[i] = k.decode() | |
except: | |
pass | |
data = dict(zip(dkeys, list(data.values()))) | |
return {stringify_nodes(key): stringify_nodes(val) for key, val in data.items()} | |
elif isinstance(data, bytes): | |
try: | |
return data.decode() | |
except: | |
return data | |
else: | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment