integer woes...

John La Rooy nospampls.jlr at doctor.com
Tue Feb 18 22:16:07 EST 2003


Luke McCarthy wrote:

> I have written an ID3v1 MP3 tag reader, but I have one slight problem. The
> track number is a two-byte integer and reads into Python like so:
>
>
> >>>tag.track
>
> '\x00\x17'
>
> (in this example, the track number is 23, which is hex 17)
>
> My dilemma: how can I convert this into an integer?
>
> int(tag.track) doesn't work!
> I get: ValueError: invalid literal for int():
>
> Whoever designed these tags must be weird. Why was the track number a 
> short
> int yet the year a string??? Why make artist, title and album 30 chars 
> long
> yet make the comment 28 chars long???
>
> Luke McCarthy
>
>
 >>> import struct
 >>> struct.unpack('>H','\x00\x17')[0]
23

Read up on the struct module, it's very handy for dealing with those 
types of files

John





More information about the Python-list mailing list