【发布时间】:2021-12-23 11:31:20
【问题描述】:
yaml_file1.yaml
container:
name: NAME1
number: 222
type:
compression:lz
function: boot_a
type_revno:REV123
type_prodno: REV345
fw:
prodno:ABC123
revno:ABC345
prodno:DEF123
revno:DEF345
url:"file:///path1"
slot : 1
compression:zip
function: boot_b
type_revno:REV111
type_prodno: REV222
fw:
prodno:XYZ111
revno:XYZ222
prodno: UVW111
revno:UVW222
url: "file:///path2"
slot : 2
compression:lz
function: appl_a
type_revno:REV333
type_prodno: REV444
fw:
prodno:XYZ333
revno:XYZ444
prodno: UVW333
revno:UVW444
url: "file:///path3"
slot : 3
compression:zip
function: appl_b
type_revno:REV333
type_prodno: REV444
type_revno:REV333
type_prodno: REV444
fw:
prodno:XYZ333
revno:XYZ444
prodno: UVW333
revno:UVW444
url: "file:///path4"
slot : 4
*********************************
yaml_file2.yaml
container:
name: NAME1
number: 222
timestamp: 1634495717
type:
compression:lz
function: boot_a
type_revno:REV123
type_prodno: REV345
fw:
prodno:ABC123
revno:ABC345
prodno:DEF123
revno:DEF345
offset: 0
md5sum : (some md5sum value)
size: 3000
slot : 1
compression:zip
function:boot_b
type_revno:REV111
type_prodno: REV222
fw:
prodno:XYZ111
revno:XYZ222
prodno: UVW111
revno:UVW222
offset : 1
md5sum : (some md5sum value)
size: 2000
slot : 2
compression:lz
function: appl_a
type_revno:REV111
type_prodno: REV222
fw:
prodno:XYZ111
revno:XYZ222
prodno: UVW111
revno:UVW222
offset: 2
md5sum : (some md5sum value)
size : 1000
slot : 3
compression:zip
function: appl_b
type_revno:REV333
type_prodno: REV444
type_revno:REV333
type_prodno: REV444
fw:
prodno:XYZ333
revno:XYZ444
prodno: UVW333
revno:UVW444
offset: 3
md5sum : (some md5sum value)
slot : 4
正如我们所见,yaml_file2.yaml 的所有 'type' 实例都缺少 url 参数。 我想从 yaml_file1.yaml 中获取“url”参数及其值,并将其用于 yaml_file2.yaml 中的“type”对应实例。 我们可以使用映射的“函数”名称来区分“类型”。
我们可以在 python 中使用字典方法做到这一点吗?
我是一个业余爱好者,还在学习。到目前为止,我只了解访问非嵌套字典。
如果我尝试访问这样的网址:
with open('yaml_file1.yaml','r') as ingen:
datagen = yaml.full_load(ingen)
for k, v in datagen["type"].items():
print (datagen["type"][k]["url"])
我明白了
Traceback (most recent call last):
File "compareyaml.py", line 43, in <module>
for k, v in datagen["type"].items():
AttributeError: 'list' object has no attribute 'items'
有人可以帮忙吗?
【问题讨论】:
标签: python dictionary nested yaml extract