Im trying to get a specific part from a string which changes from a result to another here's an example:
?v=Vc2nuekbxo:Video taken down... by Pfilip 6 months ago 38 seconds 149,492
?v=Ec2nIbkcxo:Video down... by Pfilip 8 months ago 9 minutes, 10 seconds 988,742
?v=Rc2nIbkbxo:Video up... by Pfilip 1 months ago 50 seconds 85,742
so im trying to collect the length which is 38 seconds
, 10 seconds
and 50 seconds
in this case, how can i do this knowing that the results can be diffrent each time
CodePudding user response:
Using regex
this can be obtained as (for the input you provided):
import re
data = ['?v=Vc2nuekbxo:Video taken down... by Pfilip 6 months ago 38 seconds 149,492',
'?v=Ec2nIbkcxo:Video down... by Pfilip 8 months ago 9 minutes, 10 seconds 988,742',
'?v=Rc2nIbkbxo:Video up... by Pfilip 1 months ago 50 seconds 85,742']
for item in data:
if time := re.search(r'.*\s([0-9] \s*seconds)', item):
print (time.group(1))
Output:
38 seconds
10 seconds
50 seconds