通过将文本存储为一个字符串或Python中的某个内容,是否可以对文本中的所有时间进行加减?

通过将文本存储为一个字符串或Python中的某个内容,是否可以对文本中的所有时间进行加减?,python,datetime,time,Python,Datetime,Time,我的问题与YouTube描述中列出的时间有关。如果我想刮去前面5秒的死气,比如说这首歌的描述(在从视频上刮去5秒后),我需要一个能发现并改变所有时间的函数——5秒,而不是自己做数学运算。我想将其粘贴到脚本中,并将终端的输出复制到YouTube… 我可以像这样将它存储为一个变量并对它做些什么吗 times = (""" Published on Aug 24, 2012 Band: Metallica Album: Master of Puppets Released: March 3, 1986

我的问题与YouTube描述中列出的时间有关。如果我想刮去前面5秒的死气,比如说这首歌的描述(在从视频上刮去5秒后),我需要一个能发现并改变所有时间的函数——5秒,而不是自己做数学运算。我想将其粘贴到脚本中,并将终端的输出复制到YouTube…
我可以像这样将它存储为一个变量并对它做些什么吗

times = ("""
Published on Aug 24, 2012
Band: Metallica
Album: Master of Puppets
Released: March 3, 1986
Genre: Thrash Metal

Tracks:
0:00 Battery
5:11 Master Of Puppets
13:46 Welcome Home (Sanitarium)
20:14 The Thing That Should Not Be
26:49 Disposable Heroes
35:04 Leper Messiah
40:46 Orion
49:11 Damage, Inc.
Heres a link for the lyrics
http://www.darklyrics.com/lyrics/metallica/masterofpuppets.html#1All 
Rights to Metallica!
I do not own any rights!
""")
我尝试了一些方法,但最终都涉及到大量的删除、重写、复制、重新格式化、分离等操作,但没有看到任何可以一次性更改此字符串的操作。我试图解决的问题太复杂了,没用在这里发布。我最终放弃了,一直用计算器手工修改(在一个比这个例子更复杂的视频中)。

试试这个:

import re, datetime
p = '\d+:\d+'
for i in re.finditer(p, times):
    m, s = i.group().split(':')
    if m != '0' and s != '00':
        time2 = datetime.datetime(* [1] * 4, int(m), int(s)) - datetime.timedelta(seconds=5)
        newtime = ':'.join([str(time2.minute), str(time2.second).zfill(2)])
        print(newtime)
        times = times[:i.start()] + newtime + times[i.end():]
print(times)
2017,5,5,5
只是持有者的价值观——如果有人知道更好的方法,请在评论中说出来

谨此陈辞:

import re, datetime # import modules
p = '\d+:\d+' # this is a regular expression to match digits, followed by a colon, then more digits (the format all of the times are in)
for i in re.finditer(p, times): # iterate through all of the matches found
    m, s = i.group().split(':') # split time by ':' -- this puts the first number, the minutes, into the m variable, and the seconds into the s variable
    if m != '0' and s != '00': # don't subtract at time="0:00"
        time2 = datetime.datetime(* [1] * 4, int(m), int(s)) # make a datetime to match the time of the video. The important parts are the `int(m), int(s)` to represent the minutes and seconds; the other numbers are just filler and can be changed (but not deleted)
        time2 -= datetime.timedelta(seconds=5) # subtract the five seconds
        newtime = ':'.join([str(time2.minute), str(time2.second).zfill(2)]) # put the time back into string format (zfill pads it with 0s to make it two digits)
        print(newtime)
        times = times[:i.start()] + newtime + times[i.end():] # replace the part of time with the new time. since strings are immutable we need to do this weird technique
print(times)

您应该向我们展示您在试图自己解决此问题的地方编写的代码。此外,您应该准确地告诉我们您期望的输出。输出将是相同的文本,每次减去5秒。我不能再给你看别的了。我不知道除了每次将其复制到字符串中并通过某些函数传递外,还需要做些什么,但编码的复制和粘贴同样是一件麻烦的事情,并且需要手动使用计算器进行更改。
0:00
应该如何更改-5秒?应该保持不变吗?很好。我认为这些曲目应该只有一次,而应该放在开头。它更好看,更容易使用YouTuber,让这个问题更容易解决。太棒了。我必须整天学习这个代码。我将澄清我的问题,只是为了显示代码所使用的字符串。如果愿意,您可以编辑以仅显示有效的代码。@user7914198:)今天我将在某个时候发布一个解释。我只是想把它拆开并玩一下以了解更多信息。你不必这么做。非常感谢。