gpt4 book ai didi

python - 通过python获取YAML中所有具有相同名称的节点

转载 作者:行者123 更新时间:2023-12-04 18:03:43 24 4
gpt4 key购买 nike

如何获取具有相同名称的节点的所有值,例如在 YAML 中定义的标题到 Python 的列表。

name: test
article:
title: title1
paper:
title: title2
blog:
title: title3

最佳答案

import os
import yaml

# Define the recursive function
def iter( map, match ):
output = []
for key, value in map.iteritems():
if type( value ) == dict:
output += iter( value, match )
if key == match:
output += [ value ]

return output

f = open( infile, 'r' )
data = yaml.load( f )
f.close()

print iter( data, 'title' )

关于python - 通过python获取YAML中所有具有相同名称的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30858729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com