如何获取具有相同名称的节点的所有值,例如通过python在YAML中定义到列表的标题。
name: test
article:
title: title1
paper:
title: title2
blog:
title: title3
答案 0 :(得分:0)
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' )