• 【python第三方库】easydict的使用


    一、介绍

    在 Python 中当我们需要访问字典中的元素的时候,我们需要使用类似 a['example'] 的形式来进行使用。例如现在我们有如下的字典

    d = {'foo':3, 'bar':{'x':1, 'y':2}}
    print(d['foo'])  # 如何想要访问字典的元素需要这么写
    print(d['bar']['y'])  # 如果想要继续访问字典中字典的元素需要使用二维数组
    
    • 1
    • 2
    • 3

    现在我们希望可以使用类似访问属性的方式,来访问字典里的变量,例如使用 d.foo 这种形式来访问。这个时候就可以使用 easydict 这个模块了。

    二、安装

    EasyDict允许访问dict值作为属性(递归工作)。也就是可以方便地应用 . 来访问dict的值。

    pip install easydict
    
    • 1

    三、使用

    • 示例
    from easydict import EasyDict
    
    test = {'foo':3, 'bar':{'x':1, 'y':2}}
    e_dict = EasyDict(test)
    
    print(e_dict.foo) # 3
    print(e_dict.bar.x) # 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 新增元素
    from easydict import EasyDict
    
    a = EasyDict() # 新建空的dict
    a.x = 1 #新增元素
    a.y = 2
    a.z = 'new'
    print(a) # a: {'x':1, 'y':2, 'z':'new'}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    update()函数把字典dict2的键/值对更新到dict里面。
    意思就是 把一个字典的键值对 更新到 另一个字典里。
    
    1. 实例:
    dict = {'Name": 'Zara', 'Age':7}
    dict2 ={ 'Sex': 'female' }
    dict.update(dict2)
    print "Value: %s" % dict
    
    输出:
    Value: {'Age': 7, 'Name': 'Zara', 'Sex': 'female' }
    
     
    
    2. 注意:有相同的键会直接替换成update的值:
    a = {1: 2, 2: 2}
    b = {1: 1, 3: 3}
    b.update(a)
    print (b)
    
    输出:
    {1: 2, 2: 2, 3: 3}
    
    3. 在已有的dict中增加新元素
    a = {'x':1, 'y':2, 'z':3}
    a.update(EasyDict(version = 'v1')) 
    print(a) # {'x':1, 'y':2, 'z':3, 'version':'v1'}
    
    返回值:
    该方法没有任何返回值。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • easydict转换为普通的dict
      有的时候我们需要将 easydict 转换为普通的 dict(例如使用 yaml.dump 进行保存的时候)。我们可以直接使用 dict 进行类型转换(推荐这一种方式),如下所示:
    from easydict import EasyDict as ed
    
    a = {'x':1, 'y':2, 'z':3}
    e_dict = ed(a)
    print(type(e_dict)) # easydict.EasyDict
    
    e_dict = dict(e_dict) 
    print(type(e_dict)) # dict
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    除了上面的方式之外,我们可以使用 json,首先利用 json.dumps 将 dict 转为字符串,接着使用 json.loads 将字符串转为 dict(方法二,没有上面的方式简洁)。

    • 处理 json/yaml 里的信息
    import json
    from easydict import EasyDict as Edict
    
    with open(json_path_ori, 'r') as json_cache:
         data = Edict(json.load(json_cache))
         return data
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    import yaml
    from easydict import EasyDict
    
    def setup_config():
        with open(os.path.join('test_config.yaml')) as f:
            cfg = yaml.safe_load(f) # 读取配置文件
        cfg = EasyDict(cfg) # 存成 Easydict 的格式
        return cfg
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    对象.属性与对象[属性]的区别
    第三章 ArcGIS Pro创建 python 脚本工具(二)
    Sql优化(六):如何优化order by?
    Linux Mysql使用【mysqldump】备份数据库结构及数据
    国内优质企业网盘推荐:满足您的文件存储与共享需求
    辐射威胁:揭示辐射对人体健康和肠道菌群的影响及防护
    解决:ImportError: cannot import name ‘get_config‘
    C++ Reference: Standard C++ Library reference: C Library: cstring: strcpy
    gs_moment
    深度学习的进展
  • 原文地址:https://blog.csdn.net/All_In_gzx_cc/article/details/127732706