获取所有叶节点的路径(从根节点)? [英] Fetching the path( from root node ) for all the leaf nodes?

查看:56
本文介绍了获取所有叶节点的路径(从根节点)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 python 脚本读取一个 XML 文件,以给出文件夹结构.

My python script reads a XML file, to give the Folder Structure.

我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<serverfiles name="Test">
  <serverfiles name="Fail">
    <serverfiles name="Cam1">
      <serverfiles name="Mod1">
        <serverfiles name="2019-01-07" />
        <serverfiles name="2019-01-08" />
      </serverfiles>
      <serverfiles name="Mod2">
        <serverfiles name="2019-02-07" />
        <serverfiles name="2019-02-08" />
      </serverfiles>
    </serverfiles>
  </serverfiles>
  <serverfiles name="Pass">
    <serverfiles name="Cam1">
      <serverfiles name="Mod1">
        <serverfiles name="2019-03-07" />
        <serverfiles name="2019-03-08" />
      </serverfiles>
      <serverfiles name="Mod2">
        <serverfiles name="2019-04-07" />
        <serverfiles name="2019-04-08" />
      </serverfiles>
    </serverfiles>
  </serverfiles>
</serverfiles>

Python 脚本:

from pprint import pprint
import xml.etree.ElementTree as ET

def walk(e):
    name = e.attrib['name']
    children = [walk(c) for c in e if e.tag == 'serverfiles']
    return {'name': name, 'children': children} if children else {'name': name, 'path': ''}

file = ET.parse(r'folder_structure.xml')
r = file.getroot()
s = walk(r)
pprint(s)

产生以下输出:

{'children': [{'children': [{'children': [{'children': [{'name': '2019-01-07',
                                                         'path': ''},
                                                        {'name': '2019-01-08',
                                                         'path': ''}],
                                           'name': 'Mod1'},
                                          {'children': [{'name': '2019-02-07',
                                                         'path': ''},
                                                        {'name': '2019-02-08',
                                                         'path': ''}],
                                           'name': 'Mod2'}],
                             'name': 'Cam1'}],
               'name': 'Fail'},
              {'children': [{'children': [{'children': [{'name': '2019-03-07',
                                                         'path': ''},
                                                        {'name': '2019-03-08',
                                                         'path': ''}],
                                           'name': 'Mod1'},
                                          {'children': [{'name': '2019-04-07',
                                                         'path': ''},
                                                        {'name': '2019-04-08',
                                                         'path': ''}],
                                           'name': 'Mod2'}],
                             'name': 'Cam1'}],
               'name': 'Pass'}],  'name': 'Test'}

但我想要的输出是:

{'children': [{'children': [{'children': [{'children': [{'name': '2019-01-07',
                                                         'path': '/Test/Fail/Cam1/Mod1/'},
                                                        {'name': '2019-01-08',
                                                         'path': '/Test/Fail/Cam1/Mod1/'}],
                                           'name': 'Mod1'},
                                          {'children': [{'name': '2019-02-07',
                                                         'path': '/Test/Fail/Cam1/Mod2/'},
                                                        {'name': '2019-02-08',
                                                         'path': '/Test/Fail/Cam1/Mod2/'}],
                                           'name': 'Mod2'}],
                             'name': 'Cam1'}],
               'name': 'Fail'},
              {'children': [{'children': [{'children': [{'name': '2019-03-07',
                                                         'path': '/Test/Pass/Cam1/Mod1/'},
                                                        {'name': '2019-03-08',
                                                         'path': '/Test/Pass/Cam1/Mod1/'}],
                                           'name': 'Mod1'},
                                          {'children': [{'name': '2019-04-07',
                                                         'path': '/Test/Pass/Cam1/Mod2/'},
                                                        {'name': '2019-04-08',
                                                         'path': '/Test/Pass/Cam1/Mod2/'}],
                                           'name': 'Mod2'}],
                             'name': 'Cam1'}],
               'name': 'Pass'}],  'name': 'Test'}

我已经提到了访问ElementTree节点父节点如何在解析时从python的root获取xpathxml,但无法提出解决方案.

I have referred access ElementTree node parent node and how to get xpath from root in python while parsing xml, but couldn't come up with a solution.

如何在解析 XML 文件时获取每个叶节点的路径(从根节点开始)?

How do I get the path(starting from Root Node) for each of the leaf node, while parsing a XML file?

推荐答案

您可能忘记跟踪最终路径所需的运行名称.对于通用用例,我可能是错误的,但是您给出的特定问题,下面的脚本应该可以工作.

You probably have forgotten to keep the track of the running names which was required for the final path. I might be wrong for a generic use case, but the particular problem you have given, script below should work.

from pprint import pprint
import xml.etree.ElementTree as ET


def walk(e, runningname=''):
    name = e.attrib['name']

    # TODO: checking whether this is the leaf node
    # perhaps there are better ways
    if len(e) > 0:
        runningname += f'/{name}'

    children = [walk(c, runningname) for c in e if e.tag == 'serverfiles']

    return {'name': name, 'children': children} if children else {'name': name, 'path': runningname}


file = ET.parse(r'r.xml')
r = file.getroot()
s = walk(r)
pprint(s)

这篇关于获取所有叶节点的路径(从根节点)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆