DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Extract Artist And Title From A Song Title String

  • submit to reddit
        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: + "'" +, t.strip().title()) for t in filename[:filename.rfind('.')].split('-')]