温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Parsing the root node, to fetch the entire file structure?

python - 解析根节点,以获取整个文件结构?

发布于 2020-03-27 10:31:39

我的python脚本读取XML文件,以提供文件夹结构

我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<folderstructure>
  <folder name="Fail">
    <folder name="Cam 1">
      <folder name="Mod1">
        <folder name="2019-04-09" />
      </folder>
    </folder>
    <folder name="Cam 2">
      <folder name="Mod1">
        <folder name="2019-04-09" />
      </folder>
    </folder>
  </folder>
  <folder name="Pass">
    <folder name="Cam 1">
      <folder name="Mod1">
        <folder name="2019-04-09" />
      </folder>
    </folder>
    <folder name="Cam 2">
      <folder name="Mod1">
        <folder name="2019-04-09" />
      </folder>
    </folder>
  </folder>
</folderstructure>

我编写了以下脚本,并参考了为所有叶节点获取路径(从根节点)(我之前的问题):

def walk(e, runningPath='', flag = 1):
    name = e.attrib['name']

    if len(e)>0:
            runningPath += '/' + name
    children = [walk(c, runningPath, 0) for c in e if ((e.tag == 'folderstructure' and flag==1) or (e.tag=='folder' and flag == 0))]
    print(children)
    return {'name': name, 'children': children} if children else {'name': name, 'path': runningPath + '/' + name}

但是上面的脚本产生了'None'作为输出

我想要的输出是:

{'children': [{'children': [{'children': [{'children': [{'name': '2019-04-09',
                                                         'path': '/Fail/Cam '
                                                                 '1/Mod1/2019-04-09'}],
                                           'name': 'Mod1'}],
                             'name': 'Cam 1'},
                            {'children': [{'children': [{'name': '2019-04-09',
                                                         'path': '/Fail/Cam '
                                                                 '2/Mod1/2019-04-09'}],
                                           'name': 'Mod1'}],
                             'name': 'Cam 2'}],
               'name': 'Fail'},
              {'children': [{'children': [{'children': [{'name': '2019-04-09',
                                                         'path': '/Pass/Cam '
                                                                 '1/Mod1/2019-04-09'}],
                                           'name': 'Mod1'}],
                             'name': 'Cam 1'},
                            {'children': [{'children': [{'name': '2019-04-09',
                                                         'path': '/Pass/Cam '
                                                                 '2/Mod1/2019-04-09'}],
                                           'name': 'Mod1'}],
                             'name': 'Cam 2'}],
               'name': 'Pass'}]
}

我该如何解决呢?

查看更多

查看更多

提问者
Virat
被浏览
60
Ilya Miroshnichenko 2019-07-03 21:31

如果发生某些异常,您的函数将返回None。使用块try: except可以捕获任何异常,因此您无法面对问题的原因,请尝试从代码中删除此块以查看问题,或者捕获更具体的异常。当我看到'folderstructure'没有name,你可以通过添加解决这个问题 <folderstructure name='some name'>在你的XML或设置默认名称为您的root元素。下面的代码似乎正常工作:

def walk(e, runningPath='', flag = 1):
        try:
            name = e.attrib['name']
        except KeyError:
            name = 'root'

        if len(e)>0:
            runningPath += '/' + name
        children = [walk(c, runningPath, 0) for c in e if ((e.tag == 'folderstructure' and flag==1) or (e.tag=='folder' and flag == 0))]
        print(children)
        return {'name': name, 'children': children} if children else {'name': name, 'path': runningPath + '/' + name}

发布
问题

分享
好友

手机
浏览

扫码手机浏览