4

I want to convert the duration variable from YouTube Data api?

PT1M6S --> 1:06
PT38S --> 0:38
PT58M4 --> 58:04

Here is my codes:

p['duration'] is the value from json data

duration = re.sub(r'^PT',r'',p['duration'])
duration = re.sub(r'M',r':',duration)
if (len(p['duration']) > 5 ):
    duration = re.sub(r'S',r'',duration)
else:
    duration = "0:" + re.sub(r'S',r'',duration)

Is there a simple way to do in one regex statement?

Thanks!

2 Answers 2

2

You can use

import re
strings = ['PT1M6S', 'PT38S', 'PT58M4']
rx = re.compile(r'^PT(?:(\d+)M)?(?:(\d+)S?)?$')
for s in strings:
  print(s, ' => ', rx.sub(lambda x: f"{(x.group(1) or '00').zfill(2)}:{(x.group(2) or '00').zfill(2)}", s))

See the online demo. Output:

PT1M6S  =>  01:06
PT38S  =>  00:38
PT58M4  =>  58:04

The regex demo is ^PT(?:(\d+)M)?(?:(\d+)S?)?$, it matches

  • ^PT - PT at the start of a string
  • (?:(\d+)M)? - an optional non-capturing group matching 1+ digits (capturing them into Group 1) and an M char
  • (?:(\d+)S?)? - an optional non-capturing group matching 1+ digits (capturing them into Group 2) and an optional S char
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

Comments

0

An alternative to using a regex is using parser from dateutil. It has an option fuzzy that you can use to convert your data to datetime. If you subtract midnight today from that, you get the value as a timedelta:

from dateutil import parser
from datetime import date
from datetime import datetime

lst = ['PT1M6S','PT38S', 'PT58M4']

for t in lst:
    print(parser.parse(t, fuzzy=True) - datetime.combine(date.today(), datetime.min.time()))

gives you

0:01:06
0:00:38
0:58:04

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.