在Python中,如何更改文本文件中的时间列表?

在Python中,如何更改文本文件中的时间列表?,python,Python,我有一个文本文件,它包含很多次格式为HH:MM:SS,MM 1 00:00:05,21 --> 00:00:11,53 Thank you Alex Lopez, Alex, all the Alexs and the Latinx Affinity group for 这是视频字幕的集合。我需要做的是不断地改变时间。这样,如果视频创作者想在视频的开头添加动画,所有字幕仍将与视频中的扬声器对齐。比如说,动画是5秒。 到目前为止,我的方法几乎肯定是低效的。我是编程新手,这是我第一次在现实世

我有一个文本文件,它包含很多次格式为HH:MM:SS,MM

1
00:00:05,21 --> 00:00:11,53
Thank you Alex Lopez, Alex, all the Alexs
and the Latinx Affinity group for
这是视频字幕的集合。我需要做的是不断地改变时间。这样,如果视频创作者想在视频的开头添加动画,所有字幕仍将与视频中的扬声器对齐。比如说,动画是5秒。 到目前为止,我的方法几乎肯定是低效的。我是编程新手,这是我第一次在现实世界中使用任何编程语言来解决问题。我将分享我迄今为止所做的证明尽职调查的内容,但我认为解决这一问题的正确方法比我所做的要干净得多

import re
import string

fr = open('project.txt')
text = fr.read()
regex = r"(\d+):(\d+):(\d+),(\d+)"
matches = re.finditer(regex, text, re.MULTILINE)
matchNum = 0
z=[]

def getTimeInts():

    for matchNum, match in enumerate(matches, start=1):

        print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
        for groupNum in range(0, len(match.groups())):
            groupNum = groupNum + 1
            group = match.group(groupNum)
            print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
            q = 0
            r = 0
            s = 0
            t = 0
            if groupNum == 1:
                q = group
            elif groupNum == 2:
                r = group
            elif groupNum == 3:
                s = group
            elif groupNum == 4:
                t = group
            else:
                continue
            toAppend = str(q) + str(r) + str(s) + str(t)
            z.append(toAppend)
            if (enumerate(z) == matchNum):
                return (z)
            else:
                continue
getTimeInts()
print(z)
我不知道有多少代码适合发布,所以这里是我到目前为止的整个程序。正如你所看到的,我通常在编码方面还很年轻。任何提示或更简单的功能或任何帮助都将不胜感激。提前感谢

您可以使用

基本上
1.将时间戳转换为datetime对象(比如t1)
2.创建一个5秒的timedelta对象(比如t2)
3.添加t1和t2(t1+t2)
4.以您喜欢的任何方式解析生成的datetime对象

你需要的一切都在房间里


通过在Python中使用日期和时间函数并将字符串转换为datetime对象:

from datetime import datetime, timedelta
from time import strptime
q = 7
time_string = '00:58:05'
a = datetime.strptime(time_string, '%H:%M:%S')
b = a + timedelta(minutes=q)
print(a.time())
print(b.time())
from datetime import datetime, timedelta
from time import strptime
q = 7
time_string = '00:58:05'
a = datetime.strptime(time_string, '%H:%M:%S')
b = a + timedelta(minutes=q)
print(a.time())
print(b.time())