954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Issues reading ID3 tags

I am writing a script to rename and reorganize the mp3 on my iPod. For those who don't know, if you drag and drop your music from the iPod to a computer, there is a crazy F## folder structure and all the names are 4 random characters. I want to run the script and have it read the metadata ID3 tags, the rename and redo the folder structure based on the metadata.

The issue I am running into is that the ID3 tags store hex values with a \x prefix. Python requires a 0x prefix. I like to name my files with "Artist - Album - Track - Title". I can't read the track number because it is stored with a \x prefix.

Any ideas on how to convert/read/fix this?

toadzky
Junior Poster in Training
96 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Found the solution. You have to convert from binary to hex, then decode it and give it to an int constructor. Here's how to do it in one line:

from binascii import *
value = int(b2a_hex(input).decode(), 16)
toadzky
Junior Poster in Training
96 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 
for bunchofhex in [r'\x45\x23\x67',r'\x123456']:
    print bunchofhex
    bunchofhex = bunchofhex.split(r'\x')
    print bunchofhex
    value = int('0x'+''.join(bunchofhex),0)
    print '%i = 0x%x' %(value,value)
    print '-'*40
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: