时间码标签计算



我是python的初学者,我有一个小问题。有一个纯文本计时标签(小时:分钟:秒)。"Some text 1"one_answers"Some text 3"是广告。我需要计算文本中广告的总时长。怎么做呢?

& lt;时间= ",08:00:00&quot的在& lt; type = ad>1

如果每个时间戳之前都有字符串,那么您应该使用count方法查找字符串中出现的次数,例如num = string.count("time=")

然后可以在这个范围内使用for循环来查找实际的字符串。

times = []
index = 0
for i in range(0, num):
time = string.index("time=", index)  # search for "time=", starting from index
times.append(string[time+6:time+14])
index = time+1

然后你可以在一个列表中将字符串分成小时-分钟-秒整数字符串,如下所示:

hms = [timestr.split(":") for timestr in times]
总之,下面的完整代码应该足够工作
string = '<time=”08:00:00"><type=ad> some text 1 <time=”08:02:24"></type=ad> some text 2 <time=”08:10:18"><type=ad> some text 3 <time=”08:12:20"></type=ad>'
num = string.count("time=")
times = []
index = 0
for i in range(0, num):
time = string.index("time=", index)  # search for "time=", starting from index
times.append(string[time+6:time+14])
index = time+1
print(times)
hms = [timestr.split(":") for timestr in times]
print(hms)

有任何问题请留言

最新更新