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.
This code gives me the error
I simplified the concept to see if it would work and it does, see below:
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