zoukankan      html  css  js  c++  java
  • python 中easydict库解析json文件

    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)]
    
    
  • 相关阅读:
    13 内建属性 _getattribute_ 内建函数
    12 垃圾回收GC
    11 元类
    12 动态语言 __slots__
    11 作用域
    10 带参数的装饰器 通用装饰器 类装饰器
    9 装饰器
    8 闭包
    6 生成器 yield 协程
    cmd常用命令
  • 原文地址:https://www.cnblogs.com/Henry-ZHAO/p/13343889.html
Copyright © 2011-2022 走看看