在python中查找json树中的路径

时间:2015-08-19 22:48:25

标签: python json

我有一个

形式的json树
{"reply": 0, "id": 30, "children": [{"reply": 0, "id": 451, "children": []}, {"reply": 0, "id": 307, "children": []}, {"reply": 0, "id": 276, "children": []}, {"reply": 0, "id": 253, "children": []}]}

我希望获得从root到json树叶子的所有路径。 为此,我正在使用

import json
f=open('tree_json','r')
def paths(tree, cur=()):
         if not tree:
            yield cur
         else:
            for n, s in tree.items():
                for path in paths(s, cur+(n,)):
                   yield path
for line in f:
    tree=json.loads(line)
   print list(paths(tree,(0,)))

但是,我无法在树中打印路径。我想要的输出是:      {30451},{30307},{30276}。我正在

    for n, s in tree.items():
     AttributeError: 'int' object has no attribute 'items'

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题,但是"错误"你得到的不是错误;它是path(tree, 0)返回的值,它是一个生成器。您需要将其包装在list()中以强制它自行评估。也就是说,如果你这样做,你就会遇到几个错误。即使您修复了表面错误,您的代码也无法为您提供所需的结果。

此代码将执行您尝试执行的操作:

import pprint


def paths(tree):
    if not tree['children']:
        yield (tree['id'],)
    else:
        for child in tree['children']:
            for descendant in paths(child):
                yield (tree['id'],) + descendant

tree = {
    'reply': 0,
    'id': 30,
    'children': [
        {'reply': 0, 'id': 451, 'children': []},
        {'reply': 0, 'id': 307, 'children': []},
        {'reply': 0, 'id': 276, 'children': []},
        {'reply': 0, 'id': 253, 'children': [
            {'reply': 0, 'id': 600, 'children': []},
            {'reply': 0, 'id': 700, 'children': []},
            {'reply': 0, 'id': 800, 'children': []},
            {'reply': 0, 'id': 900, 'children': []},
        ]}
        ]
    }

pprint.pprint(list(paths(tree)))

输出:

[(30, 451),
 (30, 307),
 (30, 276),
 (30, 253, 600),
 (30, 253, 700),
 (30, 253, 800),
 (30, 253, 900)]