easydict的作用:可以使得以属性的方式去访问字典的值!
from easydict import EasyDict as edict
# 字典的key就随意,单双引号皆可
d = edict({'foo':3, 'bar':{'x':1, 'y':2}})
d.foo
d.bar.x
d.foo = 4 #更新
d.foo
配合simplejson库解析json目录
from easydict import EasyDict as edict
from simplejson import loads
# json文件的key必须是双引号。
j = """
{"Buffer": 12,
"List1": [
{"type" : "point", "coordinates" : [100.1,54.9] },
{"type" : "point", "coordinates" : [109.4,65.1] },
{"type" : "point", "coordinates" : [115.2,80.2] },
{"type" : "point", "coordinates" : [150.9,97.8] }
]}
"""
d = edict(loads(j))
d.Buffer #12
d.List1[0].coordinates[1] #54.9
在线创建EasyDict格式
d = EasyDict()
d.foo = 3
d.foo
# 3
d = EasyDict(log=False)
d.debug = True
d.items()
[('debug', True), ('log', False)]