我有一个dict,我想使用simplejson转换为JSON。
如何确保我的dict的所有键都是小写的?
{
"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": ""
},
编辑:谢谢大家看看我的问题,很抱歉,我没有详细解释为什么我想要这个。这是修补JSONEmitter
的{{1}}。
答案 0 :(得分:23)
>>> 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}}
答案 1 :(得分:8)
以下是禁止设置小写密钥的解决方案:
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'
答案 2 :(得分:4)
这是我的解决方案:
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
答案 3 :(得分:4)
由于您没有清楚地提到您想要做什么:
将所有键转换为小写:
>>> y = dict((k.lower(), v) for k, v in x.iteritems())
检查按键:
>>> for k in x.iterkeys():
if k.islower():
print k, 'True'
else:
print k, 'False'
答案 4 :(得分:2)
如果你只想检查,如果它们都是小写的(你的措辞,使用“确保”,不清楚,但我怀疑这不是你想要的),你可以做紧凑的一行:
all(k.islower() for k in x.iterkeys())
答案 5 :(得分:2)
这是一个完整的解决方案
from requests import CaseInsensitiveDict
或者如果你想看代码:
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
答案 6 :(得分:1)
您可以使用python-benedict
(一个dict
子类)并标准化您的实例,以将所有字符串键转换为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()
,您的dict
将变为:
{
"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": ""
})
安装:pip install python-benedict
答案 7 :(得分:0)
用于处理值的另一种适应方法:
//1
{"id":1,"random":0.7818309762014325}
//2
{"flattened":false,"id":2}