Created
October 14, 2016 22:15
-
-
Save mikegrima/4427e151d233b06b62305736268ab902 to your computer and use it in GitHub Desktop.
Quick and dirty dict key lowercaser
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
import json | |
""" | |
This is NOT extensive in any way -- just something quick and dirty. | |
""" | |
def lowercase_keys(original, in_lists=False): | |
if in_lists and isinstance(original, list): | |
new_list = [] | |
for item in original: | |
new_list.append(lowercase_keys(item, in_lists=True)) | |
return new_list | |
if not isinstance(original, dict): | |
return original | |
staging_dict = {} | |
for item, data in original.iteritems(): | |
lower_cased = lowercase_keys(data, in_lists=in_lists) | |
staging_dict[item.lower()] = lower_cased | |
return staging_dict | |
asdf = { | |
"SomeValue": "IsThis", | |
"SeCondV": "SoemthingElse", | |
"Third": { | |
"Nested": { | |
"AnotherNested": "One", | |
"Two": "AnotherNested" | |
}, | |
"Three": "Four" | |
}, | |
"Fourth": { | |
"Some Value": [ | |
"One", | |
"twO", | |
{ | |
"Three": "Four", | |
"Five": [ | |
"6", | |
"7", | |
"8", | |
9, | |
10, | |
True, | |
-10202.33982 | |
] | |
} | |
] | |
}, | |
"Fifth": None | |
} | |
#print(json.dumps(asdf, indent=4)) | |
qwer = lowercase_keys(asdf) | |
print(json.dumps(qwer, indent=4)) | |
print() | |
tyui = lowercase_keys(asdf, in_lists=True) | |
print(json.dumps(tyui, indent=4)) | |
''' | |
This outputs: | |
$ python key_lowercaser.py | |
{ | |
"third": { | |
"nested": { | |
"anothernested": "One", | |
"two": "AnotherNested" | |
}, | |
"three": "Four" | |
}, | |
"secondv": "SoemthingElse", | |
"fifth": null, | |
"fourth": { | |
"some value": [ | |
"One", | |
"twO", | |
{ | |
"Five": [ | |
"6", | |
"7", | |
"8", | |
9, | |
10, | |
true, | |
-10202.33982 | |
], | |
"Three": "Four" | |
} | |
] | |
}, | |
"somevalue": "IsThis" | |
} | |
{ | |
"third": { | |
"nested": { | |
"anothernested": "One", | |
"two": "AnotherNested" | |
}, | |
"three": "Four" | |
}, | |
"secondv": "SoemthingElse", | |
"fifth": null, | |
"fourth": { | |
"some value": [ | |
"One", | |
"twO", | |
{ | |
"five": [ | |
"6", | |
"7", | |
"8", | |
9, | |
10, | |
true, | |
-10202.33982 | |
], | |
"three": "Four" | |
} | |
] | |
}, | |
"somevalue": "IsThis" | |
} | |
''' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment