0
0

More than 1 year has passed since last update.

yamlのエイリアスとアンカーを使って2つの配列を結合する (PyYAMLで検証)

Last updated at Posted at 2022-05-19
array1 : &array1
- a: 1
  b: 2
  c: 3
array2 : &array2
- d: 4
  e: 5
  f: 6

array :
 <<: *array1
 <<: *array2

# 結果
# {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
array1 : &array1
- a: 1
  b: 2
  c: 3
array2 : &array2
- d: 4
  e: 5
  f: 6

array :
 - <<: *array1
 - <<: *array2

# 結果
# [{'a': 1, 'b': 2, 'c': 3}, {'d': 4, 'e': 5, 'f': 6}]
array1 : &array1
- a: 1
  b: 2
  c: 3
array2 : &array2
- d: 4
  e: 5
  f: 6

array :
- *array1
- *array2

# 結果
# [[{'a': 1, 'b': 2, 'c': 3}], [{'d': 4, 'e': 5, 'f': 6}]]

検証に使ったpythnコード

import yaml

def main():

    document = """
    # ここにyamlを入れる
    """
    values = yaml.safe_load(document)
    print(yaml.dump(values))

    print(values.get('array'))

if __name__ == '__main__':
    main()
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0