代码之家  ›  专栏  ›  技术社区  ›  Houy Narun

如何使用dpath库添加新的字典值?

  •  0
  • Houy Narun  · 技术社区  · 4 年前

    我有一些python字典键值行和列,如下所示。我想做的是在行索引0和列索引0处添加一个新字段“另一个新字段”。

    dpath library 为了做到这一点,我在这里做我想做的事

        import dpath.util
        import pprint
        
        
        pp = pprint.PrettyPrinter(indent=2)
        
        
        ext_rows = [ { 'row': [ { 'column': [ { 'col': [ {'class': 'bg-success text-white'},
                                              {'field': ['text01']}]},
                                   { 'col': [ {'class': 'bg-info text-white'},
                                              {'field': ['text1']}]},
                                   { 'col': [ {'class': 'bg-secondary text-white'},
                                              {'field': ['text2']}]}]}]},
                      { 'row': [ { 'column': [ { 'col': [ {'class': 'bg-primary text-white py-3'},
                                                          {'field': ['text3']}]},
                                               { 'col': [ {'size': 6},
                                                          {'class': 'bg-primary text-white py-3'},
                                                          {'field': ['text3']}]}]}]}]
        
        # at row 0, col0 add new field 'another_new_field'
        
        dpath.util.set(ext_rows, '[0]/row/*/column/0/col/*/field', ["another_new_field"])
        
        pp.pprint(ext_rows)
    
    # RESULT IN:
    # [ { 'row': [ { 'column': [ { 'col': [ {'class': 'bg-success text-white'},
    #                                     {'field': ['another_new_field']}]},
    #                          { 'col': [ {'class': 'bg-info text-white'},
    #                                     {'field': ['text1']}]},
    #                          { 'col': [ {'class': 'bg-secondary text-white'},
    #                                     {'field': ['text2']}]}]}]},
    # { 'row': [ { 'column': [ { 'col': [ {'class': 'bg-primary text-white py-3'},
    #                                     {'field': ['text3']}]},
    #                          { 'col': [ {'size': 6},
    #                                     {'class': 'bg-primary text-white py-3'},
    #                                     {'field': ['text3']}]}]}]}]
    

    如结果所示,它不附加新字段,而是用新字段替换现有值。但我想要这样:

    [ { 'row': [ { 'column': [ { 'col': [ {'class': 'bg-success text-white'},
                                        {'field': ['text01','another_new_field']}]},
                             { 'col': [ {'class': 'bg-info text-white'},
                                        {'field': ['text1']}]},
                             { 'col': [ {'class': 'bg-secondary text-white'},
                                        {'field': ['text2']}]}]}]},
    { 'row': [ { 'column': [ { 'col': [ {'class': 'bg-primary text-white py-3'},
                                        {'field': ['text3']}]},
                             { 'col': [ {'size': 6},
                                        {'class': 'bg-primary text-white py-3'},
                                        {'field': ['text3']}]}]}]}]
    

    我怎么能做到呢?谢谢。

    1 回复  |  直到 4 年前
        1
  •  1
  •   Muslimbek Abduganiev    4 年前

    你要的是尽快拿到名单 领域 并向其添加数据:

    dpath.util.get(ext_rows[0], "/row/*/column/0/col/*/field").append("another_field")
    
    推荐文章