How to ensure that a python dict keys are lowercase?
I have a dict that I want to convert in JSON using simplejson.
How can I ensure that all the keys of my dict are lowercase ?
{
"DISTANCE": 17.059918745802999,
"name": "Foo Bar",
"Restaurant": {
"name": "Foo Bar",
"full_address": {
"country": "France",
"street": "Foo Bar",
"zip_code": "68190",
"city": "UNGERSHEIM"
},
"phone": "+33.389624300",
开发者_StackOverflow中文版 "longitude": "7.3064454",
"latitude": "47.8769091",
"id": "538"
},
"price": "",
"composition": "",
"profils": {},
"type_menu": "",
"ID": ""
},
EDIT: Thanks all to had a look at my question, I am sorry I didn't explain in detailed why I wanted this. It was to patch the JSONEmitter
of django-piston
.
>>> d = {"your": "DATA", "FROM": "above"}
>>> dict((k.lower(), v) for k, v in d.iteritems())
{'from': 'above', 'your': 'DATA'}
>>> def lower_keys(x):
... if isinstance(x, list):
... return [lower_keys(v) for v in x]
... elif isinstance(x, dict):
... return dict((k.lower(), lower_keys(v)) for k, v in x.iteritems())
... else:
... return x
...
>>> lower_keys({"NESTED": {"ANSWER": 42}})
{'nested': {'answer': 42}}
Here is a solution that forbids setting a lowercase key:
class LowerCaseDict(dict):
def __setitem__(self, key, val):
if not key.islower():
raise TypeError, "%s key must be lowercase" % key
dict.__setitem__(self, key, val)
ld = LowerCaseDict()
ld['g']='g'
Here is my solution :
def lower_key(in_dict):
if type(in_dict) is dict:
out_dict = {}
for key, item in in_dict.items():
out_dict[key.lower()] = lower_key(item)
return out_dict
elif type(in_dict) is list:
return [lower_key(obj) for obj in in_dict]
else:
return in_dict
Since you have not mentioned clearly what you want to do:
Convert all keys to lowercase:
>>> y = dict((k.lower(), v) for k, v in x.iteritems())
Check for keys:
>>> for k in x.iterkeys():
if k.islower():
print k, 'True'
else:
print k, 'False'
If you just want to check if they're all lowercase (your wording, using "ensure", is not clear, but I suspect this isn't what you want), you can do that compactly in one line:
all(k.islower() for k in x.iterkeys())
Here is a complete solution
from requests import CaseInsensitiveDict
Or if you want to see the code:
class CaseInsensitiveDict(dict):
"""Basic case insensitive dict with strings only keys."""
proxy = {}
def __init__(self, data):
self.proxy = dict((k.lower(), k) for k in data)
for k in data:
self[k] = data[k]
def __contains__(self, k):
return k.lower() in self.proxy
def __delitem__(self, k):
key = self.proxy[k.lower()]
super(CaseInsensitiveDict, self).__delitem__(key)
del self.proxy[k.lower()]
def __getitem__(self, k):
key = self.proxy[k.lower()]
return super(CaseInsensitiveDict, self).__getitem__(key)
def get(self, k, default=None):
return self[k] if k in self else default
def __setitem__(self, k, v):
super(CaseInsensitiveDict, self).__setitem__(k, v)
self.proxy[k.lower()] = k
another adaptation to handle the values:
def lower_keys(x):
if isinstance(x, str):
return (x.lower())
if isinstance(x, list):
return [lower_keys(v) for v in x]
elif isinstance(x, dict):
return dict((k.lower(), lower_keys(v)) for k, v in x.items())
else:
return x
You could use python-benedict
(a dict
subclass) and standardize your instance to convert all string keys to snake_case
.
from benedict import benedict
d = benedict({
"DISTANCE": 17.059918745802999,
"name": "Foo Bar",
"Restaurant": {
"name": "Foo Bar",
"full_address": {
"country": "France",
"street": "Foo Bar",
"zip_code": "68190",
"city": "UNGERSHEIM"
},
"phone": "+33.389624300",
"longitude": "7.3064454",
"latitude": "47.8769091",
"id": "538"
},
"price": "",
"composition": "",
"profils": {},
"type_menu": "",
"ID": ""
})
# convert all string keys to snake_case (nested dict keys included)
d.standardize()
and your dict
will become:
{
"distance": 17.059918745802999,
"name": "Foo Bar",
"restaurant": {
"name": "Foo Bar",
"full_address": {
"country": "France",
"street": "Foo Bar",
"zip_code": "68190",
"city": "UNGERSHEIM"
},
"phone": "+33.389624300",
"longitude": "7.3064454",
"latitude": "47.8769091",
"id": "538"
},
"price": "",
"composition": "",
"profils": {},
"type_menu": "",
"id": ""
})
Installation: pip install python-benedict
Documentation: https://github.com/fabiocaccamo/python-benedict
Note: I am the author of this project
精彩评论