【问题标题】:Python: Extracting key-pair values from nested dictionary in yaml_file1 and putting a key-pair in yaml_file2Python:从 yaml_file1 中的嵌套字典中提取密钥对值并将密钥对放入 yaml_file2
【发布时间】: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


    【解决方案1】:

    您可以先将 yaml 文件读入名称空间中的字典。为此,您可以在 python 中使用 yaml 包:

    import yaml
    
    with open(r'yaml_file1.yaml') as file:
        dict1 = yaml.load(file, Loader=yaml.FullLoader)
    with open(r'yaml_file2.yaml') as file:
        dict2 = yaml.load(file, Loader=yaml.FullLoader)
    

    现在您有两个字典,您可以随意使用。 您从 dict1 获得想要的值并将其插入到 dict2。

    最后你将字典写回 yaml 文件:

    with open(r'result.yaml', 'w') as file:
        res = yaml.dump(dict2, file)
    

    【讨论】:

    • 感谢阿卜杜拉齐兹。我已经完成的那部分......这是我正在寻找指导的字典部分。我正在尝试为此使用字典方法 get()、items()。 dict1.keys 输出: dict1_keys(['container', 'type']) dict2.items 以字典格式输出所有键值对
    【解决方案2】:

    Yaml 文件存储为 1 个字典和 1 个列表。

    字典:容器 列表:类型

    列表“类型”将包含每个类型的嵌套字典。

    想出了如何访问 url 并添加到 dict2:

    url_var0 = datagen["type"][0]["url"]
    url_var1 = datagen["type"][1]["url"]
    url_var2 = datagen["type"][2]["url"]
    url_var3 = datagen["type"][3]["url"]
    
    dict2["type"][0]["url"] = url_var0
    dict2["type"][1]["url"] = url_var1
    

    等等。

    我剩下的挑战是弄清楚如何为此编写一个 for 循环。

    等我弄明白后会在这里更新。

    【讨论】:

      【解决方案3】:

      哇哦! 想通了!

      for ctr, idx in zip(datagen["type"], dict2["type"]):
          idx['url'] = ctr['url']
      
      

      在到达这里之前,我写了 2 个 for 循环,嵌套的,单独的……尝试存储/分配索引。没有任何效果。 我像 C 程序员一样思考。 偶然发现 : Iterating through 2 dictionaries simulataneously

      我知道对于 Python 专家来说这可能是一件小事。但是,作为一个业余爱好者,发现 'zip' 可以同时浏览 2 个字典对我来说是一项壮举。

      谢谢!

      【讨论】:

        猜你喜欢
        • 2020-11-28
        • 2012-07-13
        • 2018-03-07
        • 2014-10-21
        • 1970-01-01
        • 1970-01-01
        • 2021-08-20
        相关资源
        最近更新 更多