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

    Join Date
    Mar 2003
    Location
    Cordova, TN USA
    Posts
    4
    Rep Power
    0

    Changing Key values of a dictionary in a 'for' loop


    I'm new to python and programming in general. I'm getting stuck on this one point. I need to change a key value for every repeat of this 'for' loop. Btw, I'm writing a Python clone of Dekagen.
    Code:
    tagData = {} # This dictionary actually has quite a few keys.
    for i in compressedTracks:
    	tagData['Title'] = "%s" % read_info['TTITLE' + `i`]
    	tag = open(i, rb)
    	tag.id3tag(i, tagData, action='create')
    	tag.apev2tag(i, tagData, action='create')
    # This is the error
    Traceback (most recent call last):
      File "/home/william/Documents/project/pykagen.py", line 110, in ?
        tagData['Title'] = "%s" % read_info['TTITLE' + `i`]
    KeyError: "TTITLE'track01.cdda.wav.mp3'"
    This works:
    Code:
    for i in range(disc_id[1]):
    	print "%.02d-%s" % (i + 1, read_info['TTITLE' + `i`])
    01-Say It Ain't So (remix)
    02-No One Else (live and acoustic)
    03-Jamie (live and acoustic)
    I'm probably totally going about this the wrong way, if someone could steer me in the right direction I would appreciate it.
    Python 2.3.4, Gentoo Linux 2.6.9 Kernel
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    The KeyError means you are looking up a non-existent value in a dictionary, so the problem is that read_info does not have a key "TTITLE'track01.cdda.wav.mp3'".

    Incidentally "%s" % read_info['TTITLE' + `i`] is redundant since it is making a copy of the string for no reason. You can safely replace it with read_info['TTITLE' + `i`].

    Dave - The Developers' Coach
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The double 'T' at the beginning of the key looks kinda like a typo to me, though I could be wrong; I'm not so sure about the use of the backticks either. If your unsure about the names of your keys or how they're formatted you could print read_info.keys().

    Anyway, you might want to archive/zip up your program and attach it to this thread if it's not too big so we can all see whats going on .

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Cordova, TN USA
    Posts
    4
    Rep Power
    0
    Thanks for the response. I wasn't sure because I couldn't find this in any documentation I read. It looks like the back ticks I used convert the 'i' integer into a string that can be added to the TTITLE string. The dictionary I'm referencing contains keys TTITLE0, TTITLE1, and TTITLE2, generated by the CDDB module depending on the number of tracks in the cd.

    I'm using modules CDDB and DiscID from:
    http://cddb-py.sourceforge.net/
    and apev2tag and id3tag from:
    http://sourceforge.net/projects/pylibape/

    The code is attached thanks.
    Attached Files
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I have to grab some support modules before I can test any of this but in your first loop your actually iterating over a list of filenames retrived by glob, so i isn't actually an int -- its a string. try changing the section above to something like this:

    Code:
    for i in range(len(compressedTracks)):
        tagData['Title'] = read_info['TTITLE' + `i`]
        ...
    This should work, although I think your a take-off of the original working loop would be a much better idea:

    Code:
    for i in range(disc_id[1]):
        tagData['Title'] = read_info['TTITLE' + `i`]
        ...
    Let me know if you have any problems and I'll see what I can do,

    take care .

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Cordova, TN USA
    Posts
    4
    Rep Power
    0
    Your second option should work, although I eventually want the user to be able to select the specific tracks he/she wants to rip from the cd. Instead of all by default. I guess the problem with the method I'm trying to achieve is that it gives no room to move in this direction. In other words:

    I have dictionary of track names with keys; TTITLE0, TTITLE1, TTITLE2 ...
    I have a list of tracks; track1.mp3, track2.mp3, track3.mp3
    I have one variable named 'Title' that needs to be assigned a value, depending on what track title or track filename it corresponds to.

    I fear the problem I'm going to run into eventually. If i only rip track2.cdda.wav, then convert it to track2.cdda.wav.mp3, I will still have TTITLE + `i` but compressedTrack[0] won't reffer to track1.cdda.wav.mp3, it will refer to the second track that I ripped.

    I fear that my problem has become much larger than I intended my original question to be. My problem is as much syntax, as it is a need to know what procedure you can suggest.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    ...If i only rip track2.cdda.wav, then convert it to track2.cdda.wav.mp3, I will still have TTITLE + `i` but compressedTrack[0] won't reffer to track1.cdda.wav.mp3, it will refer to the second track that I ripped...
    Not seeing a major problem here, why don't you just update the value stored in your list or dictionary to refer to the track you want? This should be as simple as assigning the new filename to your list/dictionary after the track has been successfully ripped.

    Maybe I'm missing something ,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo