Python ElementTree解析顺序

问题描述 投票:0回答:1

我有一个非常新手的问题,但我刚刚开始,这个问题困扰着我的大脑。

这是我的XML:

<SCHEDULE>
  <PROGRAMMES>
    <PROGRAMME START="00:30:00" END="00:31:00">
      <TITLES>
        <TITLE>Name 1</TITLE>
      </TITLES>
    </PROGRAMME>
    <PROGRAMME START="00:31:00" END="00:32:00">
      <BLOCK>
        <BLOCK_NAME>Block1</BLOCK_NAME>
        <BLOCK_START>00:31:00</BLOCK_START>
      </BLOCK>
      <TITLES>
        <TITLE>Name 2</TITLE>
      </TITLES>
    </PROGRAMME>
    <PROGRAMME START="00:40:00" BILLEDEND="00:45:00">
      <BLOCK>
        <BLOCK_NAME>Block 1</BLOCK_NAME>
        <BLOCK_START>00:31:00</BLOCK_START>
      </BLOCK>
      <TITLES>
        <TITLE>Name 3</TITLE>
      </TITLES>
    </PROGRAMME>
    <PROGRAMME START="00:45:00" END="00:50:00">
      <TITLES>
        <TITLE>Name 4</TITLE>
      </TITLES>
    </PROGRAMME>
  </PROGRAMMES>
</SCHEDULE>

这是我的Python代码:

import xml.etree.ElementTree as ET

try:
    import textwrap
    textwrap.indent
except AttributeError: 
    def indent(text, amount, ch=' '):
        padding = amount * ch
        return ''.join(padding+line for line in text.splitlines(True))
else:
    def indent(text, amount, ch=' '):
        return textwrap.indent(text, amount * ch)

tree= ET.parse("block.xml")
root=tree.getroot()

for sub in root.findall('.//PROGRAMME'):

    time = sub.get("START")[:5] + " "
    nas = sub.find(".//TITLE").text

    if sub.find(".//BLOCK_NAME") == None:
        block = time + nas
    else:
        block = sub.find(".//BLOCK_NAME").text
        c= indent(time + nas,3)
        block = time + block + "\n" + c

    print(block)

结果我得到:

>>>00:30 Name 1
>>>00:31 Block1
>>>   00:31 Name 2
>>>00:40 Block 1
>>>   00:40 Name 3
>>>00:45 Name 4

现在的问题是:我错过了什么,所以我会得到这个结果

>>>00:30 Name 1
>>>00:31 Block1
>>>   00:31 Name 2
>>>   00:40 Name 3
>>>00:45 Name 4

我认为我需要以某种方式合并BLOCK_START,但我不知道如何... Tnx提前寻求帮助

python printing elementtree
1个回答
0
投票

我自己想出来了。刚刚比较了START和STARTBLOCK。

if sub.find(".//BLOCK_NAME") is None:
    block = time + nas
else:
    if sub.find(".//BLOCK_START").text != sub.get("START"):
        block = indent(time + nas, 4)
    else:
        block = sub.find(".//BLOCK_NAME").text
        c = indent(time + nas, 4)
        block = time + block + "\n" + c
© www.soinside.com 2019 - 2024. All rights reserved.