【问题标题】:YAML for python dictionaries: copying/referencing blockspython 字典的 YAML:复制/引用块
【发布时间】:2011-08-18 14:49:44
【问题描述】:

我正在编写一个包含一些配置数据的 YAML 文件。它将作为字典的字典读入 Python。一些数据需要在不同的键下重复。有没有办法在不进行大量剪切和粘贴的情况下做到这一点?

以下是 yaml 文件的示例:

BLOCK1:
  a: 1
  b: 2
  c: 3

BLOCK2:
  a: 4
  b: 5
  c: 6

BLOCK3: # Basically the same as BLOCK2
  a: 4  # Is there a way to make this a link to BLOCK2 or a copy of BLOCK2?
  b: 5
  c: 6

【问题讨论】:

    标签: yaml


    【解决方案1】:

    是的,有。看一看:http://pyyaml.org/wiki/PyYAMLDocumentation#Aliases

    基本上,你应该这样做:

    BLOCK1: 
      a: 1
      b: 2
      c: 3
    
    BLOCK2: &block
      a: 4
      b: 5
      c: 6
    
    BLOCK3: *block
    

    结果将是:

    {'BLOCK1': {'a': 1, 'b': 2, 'c': 3},
     'BLOCK2': {'a': 4, 'b': 5, 'c': 6},
     'BLOCK3': {'a': 4, 'b': 5, 'c': 6}}
    

    【讨论】:

      猜你喜欢
      • 2021-05-13
      • 2021-06-26
      • 1970-01-01
      • 2019-03-16
      • 2012-09-10
      • 1970-01-01
      • 2020-11-19
      • 2020-09-18
      • 1970-01-01
      相关资源
      最近更新 更多