如何使用Python将YAML文件中所有名称相同的节点值(例如title)获取到列表中。
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' )
{'title' : { 'article' : 'title1', 'paper' : 'title2', 'blog' : 'title3' }}
- stevenviola