Is it possible to add or subtract from all tenses in a text while storing it as a single line or something with Python?

My problem refers to the times listed in the YouTube descriptions. If I want to shave 5 seconds of dead air from the front, say this is a track description (after shaving for 5 seconds from the video), I need a function that will notice and change all the time -5 seconds instead of doing the math myself. I would like to paste it into a script and copy the output from the terminal to paste it to YouTube ...
Can I save it as a variable like this and do something?

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!
""")

      

I tried a few things, but they all ended up involving a lot of deleting, overwriting, copying, reformatting, splitting, etc., but didn't see anything that just the changes I would like to use for this line could do. one pass. My attempts to solve it would be too complicated and useless to post here. I ended up giving up and changing all the time with a calculator (in a more complex video than this example).

+3


source to share


1 answer


Try the following:

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

are just the values ​​of the holders - if anyone knows a better way, tell me in the comments.



With comments:

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)

      

+4


source







All Articles