Weird errors when trying to access a dictionary key

robinsiebler at gmail.com robinsiebler at gmail.com
Fri Jul 20 14:36:13 EDT 2007


I have a data structure that looks like this:

#    dates = {'2007': {'25': {'06/23/07': {'aerosmith': [{'sweet
emotion': 1}, {'dream on': 2}],
#                                          'Metallica': [{'Fade to
Black': 1}, {'Master of Puppets': 1}]},
#             'last_song': [Master of Puppets', 'Fade to Black',
'sweet emotion']}}}

I have a section where I try to loop through a set of dates and print
out all of the songs for a given artist:

    if type == 'artist':
        rpt_file.writelines('Song Summary for ' + artist + '\n\n')
        for year in sorted(rpt_dates):
            for week in sorted(rpt_dates[year]):
                for date in sorted(dates[year][week]):
                    if artist in dates[year][week][date]:
                        report.append(date)
                        for song in sorted(dates[year][week][date]
[artist]):
                            rpt_file.writelines('\t' + [song].keys() \
                                                + '\t' +
[song].values() + '\n')


I get the following error:

Traceback (most recent call last):
  File "C:\Program Files\ActiveState Komodo 3.5\lib\support\dbgp
\pythonlib\dbgp\client.py", line 1843, in runMain
    self.dbg.runfile(debug_args[0], debug_args)
  File "C:\Program Files\ActiveState Komodo 3.5\lib\support\dbgp
\pythonlib\dbgp\client.py", line 1538, in runfile
    h_execfile(file, args, module=main, tracer=self)
  File "C:\Program Files\ActiveState Komodo 3.5\lib\support\dbgp
\pythonlib\dbgp\client.py", line 596, in __init__
    execfile(file, globals, locals)
  File "C:\scripts\python\Songs\song_report.py", line 136, in <module>
    if __name__== '__main__': main()
  File "C:\scripts\python\Songs\song_report.py", line 134, in main
    print_rpt(options.type, rpt_dates, dates, args)
  File "C:\scripts\python\Songs\song_report.py", line 107, in
print_rpt
    + '\t' + [song].values() + '\n')
AttributeError: 'list' object has no attribute 'keys'

Here is where it gets weird:

type(song)
Traceback (most recent call last):
  File "C:\Program Files\ActiveState Komodo 3.5\lib\support\dbgp
\pythonlib\dbgp\client.py", line 3241, in runcode
    locals = self.frame.f_locals)
  File "C:\Program Files\ActiveState Komodo 3.5\lib\support\dbgp
\pythonlib\dbgp\client.py", line 1583, in runcode
    h_exec(code, globals=globals, locals=locals, module=module)
  File "C:\Program Files\ActiveState Komodo 3.5\lib\support\dbgp
\pythonlib\dbgp\client.py", line 520, in __init__
    exec code in globals, locals
  File "<console>", line 0, in <module>
TypeError: 'str' object is not callable
song
{"I Don't Wanna Stop": 1}
song.keys()
["I Don't Wanna Stop"]

For the complete script and data file, go to
http://members.dslextreme.com/users/robinsiebler/python/




More information about the Python-list mailing list