2

I Was looking for a way to scan mp3 meta-data yesterday and i found this code snippet on the Internet

def getID3(filename):
    fp = open(filename, 'r') 
    fp.seek(-128, 2)
    fp.read(3) # TAG iniziale
    title   = fp.read(30)
    artist  = fp.read(30) 
    album   = fp.read(30) 
    fp.close()
    return {'title':title, 'artist':artist, 'album':album}

it totally works and all but the problem is that every time i use it, this --->\x00 <---shows up at the end of the title, album, or artist. For example;

>>> import getid as id
>>> import os
>>> music = 'D:/Muzic'
>>> os.chdir(music)
>>> meta = id.getID3('04 - Mayday Parade - Your Song.mp3')
>>> meta
{'album': 'Tales Told By Dead Friends\x00\x00\x00\x00', 'artist': 'Mayday Parade\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'title': 'Your Song\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'}

Anyone Know how to get rid of it?

btw it happens to all of the music i currently tried.

  • 2
    Possibly better choice: use an existing metadata tagging library like Mutagen that already handles all of the different metadata variants that are seen in the wild: http://code.google.com/p/mutagen/ – bgporter Jun 01 '12 at 12:07
  • No thanks, i just need the basics like reading the title, album or artist... Nothing complicated. –  Jun 01 '12 at 12:30

2 Answers2

4

Add rstrip('\x00') to the lines where you read the file:

fp.read(30).rstrip('\x00')

Example:

>>> 'abc\x00\x00\x00\x00'.rstrip('\x00')
'abc'
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • one quick question before deciding the best answer.your and Pev's answer are pretty much the same. except yours say `rstrip('\x00')` while Pev's say `strip\x00'` whats the difference? –  Jun 01 '12 at 12:26
  • 1
    `strip` strips from the start and end of a string where as `rstrip` just strips from the end of a string. There is also an `lstrip` that just strips from the start of the string – GP89 Jun 01 '12 at 12:53
  • @Firetryer - if you know you will be stripping from the end only, it is better to use `rstrip`. The code explains itself afterwards: "I am stripping the characters from the end". – eumiro Jun 01 '12 at 13:29
  • Oh, Okay. Then i guess im using rstrip. Thanks! –  Jun 02 '12 at 09:55
1

You are reading a fixed field width (30) and the structure is padding the fields with null (\x00).

I think you can just use .strip('\x00') e.g.

   title   = fp.read(30).strip('\x00')
   artist  = fp.read(30).strip('\x00')
   album   = fp.read(30).strip('\x00')
Simon Peverett
  • 4,128
  • 3
  • 32
  • 37