Python key error

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Ok so I have the following code, ReadMyData passes a tuple of dictionaries to InsertChannelPrograms, which then builds a values tuple and appends it to a list depending on how big the input tuple (listDict) was.

PHP:
def ReadMyData(self,datFile):
   channelID = datFile[:-4]
   fields = ["PROGTITLE", "SUBTITLE",
              "EPISODE", "YEAR", "DIRECTOR",
              "PERFORMERS","PREMIERE", "FILM",
              "REPEAT", "SUBTITLES", "WIDESCREEN",
              "NEWSERIES","DEAFSIGNED", "BNW",
              "STARRATING", "CERTIFICATE", "GENRE",
              "DESCRIPTION","CHOICE", "DATE",
              "STARTTIME", "ENDTIME", "DURATION"]
   delim    = '~'
   
   lineReader   = csv.DictReader(open("./input/" + datFile,'rb'),
                  delimiter=delim,fieldnames=fields)
   
   # Read the header lines
   lineReader.next()
   lineReader.next()
   
   channelPrograms = []
   
   for row in lineReader:
      channelPrograms.append(row)
   
   for i in range(len(channelPrograms)):
      if (i+3) < len(channelPrograms):
         self.InsertChannelProgrammes(channelID,(channelPrograms[i],
         channelPrograms[i+1],channelPrograms[i+2],channelPrograms[i+3]))
      elif (i+2) < len(channelPrograms):
         self.InsertChannelProgrammes(channelID,(channelPrograms[i],
         channelPrograms[i+1],channelPrograms[i+2]))
      elif (i+1) < len(channelPrograms):
         self.InsertChannelProgrammes(channelID,(channelPrograms[i],
         channelPrograms[i+1]))
      else:
         self.InsertChannelProgrammes(channelID,(channelPrograms[i]))

def InsertChannelProgrammes(self,channelID,listDict):
   sqlString = """INSERT INTO CHANNELPROGRAMME(CHANNELID,DTE,STARTTIME) VALUES(%s,%s,%s)"""
   columns = []
   for i in range(len(listDict)):
      tempDict = listDict[i]
      valuesTuple = (channelID,self.FormatDate(tempDict["DATE"]),tempDict["STARTTIME"])
      columns.append(valuesTuple)
   if len(columns) > 0:
      self._cursor.executemany(sqlString,columns)

This code gives me the error

Traceback (most recent call last):
File "noThread.py", line 82, in <module>
processing.ReadMyData(channelFile+'.dat')
File "noThread.py", line 52, in ReadMyData
self.InsertChannelProgrammes(channelID,(channelPrograms))
File "noThread.py", line 58, in InsertChannelProgrammes
tempDict = listDict
KeyError: 0


I simplified the concept to see if it would work and it does, see below:

PHP:
>>> dateList = []
>>>
>>> dateDict1 = {'DATE': '2010-06-20'}
>>> dateDict2 = {'DATE': '2010-06-28'}
>>>
>>> dateList.append(dateDict1)
>>> dateList.append(dateDict2)
>>>
>>> def PassList():
...    TakeList((dateList[0],dateList[1]))
...
>>> def TakeList(listDict):
...    for i in range(len(listDict)):
...       tempDict = listDict[i]
...       print tempDict["DATE"]
...
>>> PassList()
2010-06-20
2010-06-28
 
Slight update, I added some print statements to debug the issue

PHP:
def ReadMyData(self,datFile):
   ...
   ...
   for i in range(len(channelPrograms)):
      if (i+3) < len(channelPrograms):
         print "Inserting 4"
         self.InsertChannelProgrammes(channelID,(channelPrograms[i],
         channelPrograms[i+1],channelPrograms[i+2],channelPrograms[i+3]))
      elif (i+2) < len(channelPrograms):
         print "Inserting 3"
         self.InsertChannelProgrammes(channelID,(channelPrograms[i],
         channelPrograms[i+1],channelPrograms[i+2]))
      elif (i+1) < len(channelPrograms):
         print "Inserting 2"
         self.InsertChannelProgrammes(channelID,(channelPrograms[i],
         channelPrograms[i+1]))
      else:
         print "Inserting 1, i is " +str(i) +" length is " +str(len(channelPrograms))
         print "Last date is " +channelPrograms[i]["DATE"]
         self.InsertChannelProgrammes(channelID,(channelPrograms[i]))

def InsertChannelProgrammes(self,channelID,listDict):
   sqlString = """INSERT INTO CHANNELPROGRAMME(CHANNELID,DTE,STARTTIME) VALUES(%s,%s,%s)"""
   columns = []
   for i in range(len(listDict)):
      print "Dictionary " + str(i) +" " +listDict[i]["DATE"]
   if len(columns) > 0:
      pass

The last bit of the output is

Inserting 4
Dictionary 0 17/10/2010
Dictionary 1 17/10/2010
Dictionary 2 18/10/2010
Dictionary 3 18/10/2010
Inserting 3
Dictionary 0 17/10/2010
Dictionary 1 18/10/2010
Dictionary 2 18/10/2010
Inserting 2
Dictionary 0 18/10/2010
Dictionary 1 18/10/2010
Inserting 1, i is 196 length is 197
Last date is 18/10/2010
Traceback (most recent call last):
File "noThread.py", line 89, in <module>
processing.ReadData(channelFile+'.dat')
File "noThread.py", line 57, in ReadData
self.InsertChannelProgrammes(channelID,(channelPrograms))
File "noThread.py", line 63, in InsertChannelProgrammes
print "Dictionary " + str(i) +" " +listDict["DATE"]
KeyError: 0
 
I found the problem, the last else wasn't actually passing a tuple apparently it needed to be

PHP:
      else:
         self.InsertChannelProgrammes(channelID,(channelPrograms[i],))
 
Back
Top Bottom