python 根据字典值求和帧
python sum frames from dictionary value
我有 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)
这是一个想法:还包括 google 协作 link:https://colab.research.google.com/drive/1-adWbqegysVqUE32m_IuxAKEZyk_pXZb#scrollTo=6RCpr5fkfHBh
我们的想法是使用默认字典,它允许您灵活地按某个键分组并在该键出现时递增其值。
from datetime import datetime
from bs4 import BeautifulSoup
from collections import defaultdict
data = """<starttime>02-02-2021 01:30:52:154</starttime><frames>94</frames>
<starttime>02-02-2021 01:50:52:124</starttime><frames>96</frames>
<starttime>02-02-2021 02:50:52:124</starttime><frames>120</frames>
<starttime>02-02-2021 02:50:52:124</starttime><frames>180</frames>"""
hour_sums = defaultdict(int)
rows = data.split('\n')
for row in rows:
soup = BeautifulSoup(row, 'html.parser')
time = soup.find('starttime').get_text()
hour_piece = time.split(' ')[1].split(':')[0]
frames = int(soup.find('frames').get_text())
hour_sums[hour_piece] += frames
Then the output is:
defaultdict(int, {'01': 190, '02': 300})
我有 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)
这是一个想法:还包括 google 协作 link:https://colab.research.google.com/drive/1-adWbqegysVqUE32m_IuxAKEZyk_pXZb#scrollTo=6RCpr5fkfHBh
我们的想法是使用默认字典,它允许您灵活地按某个键分组并在该键出现时递增其值。
from datetime import datetime
from bs4 import BeautifulSoup
from collections import defaultdict
data = """<starttime>02-02-2021 01:30:52:154</starttime><frames>94</frames>
<starttime>02-02-2021 01:50:52:124</starttime><frames>96</frames>
<starttime>02-02-2021 02:50:52:124</starttime><frames>120</frames>
<starttime>02-02-2021 02:50:52:124</starttime><frames>180</frames>"""
hour_sums = defaultdict(int)
rows = data.split('\n')
for row in rows:
soup = BeautifulSoup(row, 'html.parser')
time = soup.find('starttime').get_text()
hour_piece = time.split(' ')[1].split(':')[0]
frames = int(soup.find('frames').get_text())
hour_sums[hour_piece] += frames
Then the output is:
defaultdict(int, {'01': 190, '02': 300})