【发布时间】:2021-06-04 16:25:24
【问题描述】:
我有一个包含某种播放列表的 xml 文件 当我解析它时,我想对帧求和。
XML 看起来像这样:
<PLAYLIST>
<type>COMMERCIAL</type>
<frames>94</frames>
<starttime>02-02-2021 01:30:55:914</starttime>
</PLAYLIST>
<PLAYLIST>
<type>COMMERCIAL</type>
<frames>96</frames>
<starttime>02-02-2021 01:35:55:914</starttime>
</PLAYLIST>
<PLAYLIST>
<type>COMMERCIAL</type>
<frames>120</frames>
<starttime>02-02-2021 02:30:55:914</starttime>
</PLAYLIST>
<PLAYLIST>
<type>COMMERCIAL</type>
<frames>180</frames>
<starttime>02-02-2021 02:40:55:914</starttime>
</PLAYLIST>
我想添加到字典中并计算特定小时的帧数,因此我将日期、分钟、秒...键,因为最后一个值会覆盖之前的所有值。 所以我不知道如何将所有帧与该键相加。
结果应该是
{'01':'190','02':'300'}
import os
import xml.etree.ElementTree as ET
from timecode import Timecode
file = '/home/Myxml.xml'
dom = ET.parse(file)
lista = dom.findall('PLAYLIST')
listXML = []
for l in lista:
type_md = l.find('type') # COMMERCIAL
start_time = l.find('starttime') # početak emitiranja
frames = l.find('frames') # TC Duration
if type_md.text == 'COMMERCIAL':
listXML.append(start_time.text[11:-10])
listXML.append(frames.text)
listXML = list(zip(listXML[::2], listXML[1::2]))
clock_dict = {}
for x in listXML:
x_dict = list(zip(x[::2], x[1::2]))
clock_dict.update(x_dict)
print(clock_dict)
# print(clock_dict)
【问题讨论】:
-
您将不得不提供更多信息并使其可重现以获得好的答案(没有人可以将其复制到他们的编辑器中并运行它)。
dom.findall()使用的是什么库?
标签: python dictionary time sum append