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

时间:2015-06-16 04:20:28

标签: python yaml

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

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

1 个答案:

答案 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' )
相关问题