我有一个非常新手的问题,但我刚刚开始,这个问题困扰着我。
这是我的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提前寻求帮助
答案 0 :(得分: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