Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Extract Artist And Title From A Song Title String

DZone's Guide to

Extract Artist And Title From A Song Title String

·
Free Resource
Extract the artist and the title of the song from a string formatted as: ARTIST - TITLE.EXT
This code strips the extension, splits the string, trims each part and capitalize it correctly (fixed pythons string.title() bug for handling apostrophes)


#filename = "rihanna - please don't stop the music.pdf"
artist, title = [re.sub("([a-zA-Z])'([A-Z])", lambda m: m.group(1) + "'" + m.group(2).lower(), t.strip().title()) for t in filename[:filename.rfind('.')].split('-')]
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}