easydict:用属性的方法获取值

easydict

EasyDict可以让你像访问属性一样访问dict里的变量。

dic = {
    "name":"regina",
    "info":{
        "age":23,
        "addr":"shanxi"
    }
}

print(dic["name"],dic['info']['age'])

正常情况下是这样访问字典内部数据的,但是我们希望可以通过dic.info.age的方式获取会更方便一点,就要用到easydict模块

from easydict import EasyDict as edict
dic = {
    "name":"regina",
    "info":{
        "age":23,
        "addr":"shanxi"
    }
}

easydic = edict(dic)
print(easydic.name)
print(easydic.info.age)
posted @ 2023-06-13 15:51  ivanlee717  阅读(35)  评论(0编辑  收藏  举报