Weird errors when trying to access a dictionary key

Zentrader zentraders at
Fri Jul 20 15:27:14 EDT 2007

>                             rpt_file.writelines('\t' + [song].keys() \
>                                                 + '\t' +
> I get the following error:
> Traceback (most recent call last):
> AttributeError: 'list' object has no attribute 'keys'

All of these messages are correct.  The first error is
AttributeError: 'list' object has no attribute 'keys'
You are converting the dictionary to a list on this line, and lists do
not have keys
>                             rpt_file.writelines('\t' + [song].keys() \
                                                   ---> to a list <---
When you print it, you are printing a dictionary, so it does have
keys.  Note the first line has braces, not brackets so it is a
dictionary.  You might want to consider using a dictionary of classes,
with each class containing all of the data that is now in the
hodgepodge of dictionaries, or simply a list with item[0]=year,
item[1]=month, etc.  This is an over-simplified version of using
classes to store data.  There has to be more examples on the web.
[code].class my_class :
.   def __init__(self):
.      self.field_1 = "init field_1"
.      self.field_2 = 0
.objectList = []
.for j in range(0, 2):
.    objectList.append( my_class() )
.objectList[0].field_1= "Test [0] Field 1"
.objectList[0].field_2= "Data for Field #2 for [0]"
.objectList[0].field_3= "Data for Field #3 for [0]"  ## not in

print "objectList[0] =", objectList[0].field_1, "---->",
       objectList[0].field_2, "---->",
print "objectList[1] =", objectList[1].field_1, "---->",

More information about the Python-list mailing list