mercredi 13 novembre 2019

Python - split, regex and condition

I have a target artist and would like to fetch its correspondent id, like so:

import re
target = 'Portishead'
videos = ['Portishead - Roads (Vg1jyL3cr60)', 'Portishead - Roads - (WQYsGWh_vpE)', 'Need For Speed (Linkin Park - Roads Untraveled) Music Video (7Lkq7bf6kU8)', 'Lawson - Roads (I-SOaSU0ieA)', 'Vargas & Lagola - Roads (Audio) (Kd3s20GmPVE)']

for item in videos:
    artist = item.split('-')[0]
    # here I get whats inside parenthesis, not always an id
    video_id = re.findall('\(([^)]+)', item)
    # and here the id, which is always the last split item
    id_ = (video_id[-1])
    if artist == target:
       print id_

but my if condition is not working for the target artist. I print no results.

what is the best way of achieving this using a for loop or otherwise, considering that the real list is very large?

I want to fetch above "Vg1jyL3cr60"

Aucun commentaire:

Enregistrer un commentaire