Thread: Parse .m3u file

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Parse .m3u file


    I am trying to read through this .m3u file:


    #EXTM3U
    #EXTINF:161,Murderers - John Frusciante
    /Users/Administrator/Music/iTunes/iTunes Media/Music/John Frusciante/To Record Only Water For Ten Days/07 Murderers.m4a
    #EXTINF:255,Clutter - Ronald Jenkees
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Ronald Jenkees/Ronald Jenkees/04 Clutter.mp3
    #EXTINF:258,We Get High - Logic
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Compilations/Young Sinatra_ Undeniable/12 We Get High.mp3
    #EXTINF:274,6 A kiss - Bad Meets Evil
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Bad Meets Evil/Helll - The Sequel/6 A kiss.m4a
    #EXTINF:237,Track 11 -
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/11 Track 11.m4a
    #EXTINF:466,Pledge Allegiance (Feat. Rick - T.I.
    /Users/Administrator/Music/iTunes/iTunes Media/Music/T.I_/No Mercy/19 Pledge Allegiance (Feat. Rick.mp3
    #EXTINF:209,Track 08 -
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/08 Track 08.m4a
    #EXTINF:261,Time of My Life - Macy Gray
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Compilations/8 Mile/09 Time of My Life.mp3
    #EXTINF:276,Empire State Of Mind (Featuring Alicia Keys) - Jay-Z
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Jay-Z/The Blueprint 3/05 Empire State Of Mind (Featuring Alicia Keys).mp3
    #EXTINF:222,The Killers / When You Were Young - The Killers
    /Users/Administrator/Music/iTunes/iTunes Media/Music/The Killers/Greatest Hits/1-03 The Killers _ When You Were Young.mp3
    #EXTINF:210,You Wish - Nightmares On Wax
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Nightmares On Wax/In A Space Outta Sound/06 You Wish.m4a
    #EXTINF:199,Connect For - Common Market
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Common Market/Common Market/04 Connect For 1.mp3
    #EXTINF:321,Moretime - Nightmares On Wax
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Nightmares On Wax/Thought So.._/07 Moretime.mp3
    #EXTINF:469,Blame Game (feat. John Legend) - Kanye West
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Kanye West/My Beautiful Dark Twisted Fantasy/11 Blame Game (feat. John Legend).mp3

    I would like each line to be read into a list as separate elements.

    Code:
    print m3u.readline()
    Outputs:
    /Users/Administrator/Music/iTunes/iTunes Media/Music/Kanye West/My Beautiful Dark Twisted Fantasy/11 Blame Game (feat. John Legend).mp3

    Code:
    print m3u.readlines()
    Outputs:
    ['#EXTM3U\r#EXTINF:161,Murderers - John Frusciante\r/Users/Administrator/Music/iTunes/iTunes Media/Music/John Frusciante/To Record Only Water For Ten Days/07 Murderers.m4a\r#EXTINF:255,Clutter - Ronald Jenkees\r/Users/Administrator/Music/iTunes/iTunes Media/Music/Ronald Jenkees/Ronald Jenkees/04 Clutter.mp3\r#EXTINF:258,We Get High - Logic\r/Users/Administrator/Music/iTunes/iTunes Media/Music/Compilations/Young Sinatra_ Undeniable/12 We Get High.mp3\r#EXTINF:274,6 A kiss - Bad Meets Evil\r/Users/Administrator/Music/iTunes/iTunes Media/Music/Bad Meets Evil/Helll - The Sequel/6 A kiss.m4a\r#EXTINF:237,Track 11 - \r/Users/Administrator/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/11 Track 11.m4a\r#EXTINF:466,Pledge Allegiance (Feat. Rick - T.I.\r/Users/Administrator/Music/iTunes/iTunes Media/Music/T.I_/No Mercy/19 Pledge Allegiance (Feat. Rick.mp3\r#EXTINF:209,Track 08 - \r/Users/Administrator/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/08 Track 08.m4a\r#EXTINF:261,Time of My Life - Macy Gray\r/Users/Administrator/Music/iTunes/iTunes Media/Music/Compilations/8 Mile/09 Time of My Life.mp3\r#EXTINF:276,Empire State Of Mind (Featuring Alicia Keys) - Jay-Z\r/Users/Administrator/Music/iTunes/iTunes Media/Music/Jay-Z/The Blueprint 3/05 Empire State Of Mind (Featuring Alicia Keys).mp3\r#EXTINF:222,The Killers / When You Were Young - The Killers\r/Users/Administrator/Music/iTunes/iTunes Media/Music/The Killers/Greatest Hits/1-03 The Killers _ When You Were Young.mp3\r#EXTINF:210,You Wish - Nightmares On Wax\r/Users/Administrator/Music/iTunes/iTunes Media/Music/Nightmares On Wax/In A Space Outta Sound/06 You Wish.m4a\r#EXTINF:199,Connect For - Common Market\r/Users/Administrator/Music/iTunes/iTunes Media/Music/Common Market/Common Market/04 Connect For 1.mp3\r#EXTINF:321,Moretime - Nightmares On Wax\r/Users/Administrator/Music/iTunes/iTunes Media/Music/Nightmares On Wax/Thought So.._/07 Moretime.mp3\r#EXTINF:469,Blame Game (feat. John Legend) - Kanye West\r/Users/Administrator/Music/iTunes/iTunes Media/Music/Kanye West/My Beautiful Dark Twisted Fantasy/11 Blame Game (feat. John Legend).mp3\r']

    Where are the extra characters coming from? Are there no newline characters at the end of the lines to let python know that they actually are separate lines?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    This may be close enough to what you need.


    Code:
    import pprint
    
    with open('a') as INF:
        MUSIC_FILES = [LINE.strip() for LINE in INF.readlines() if not LINE.startswith('#')]
    
    pprint.pprint(MUSIC_FILES)
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo