我有一些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']}]}]}]}]
dpath.util.set(ext_rows, '[0]/row/*/column/0/col/*/field', ["another_new_field"])
pp.pprint(ext_rows)
如结果所示,它不附加新字段,而是用新字段替换现有值。但我想要这样:
[ { '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']}]}]}]}]
我怎么能做到呢?谢谢。