I am getting a "KeyError
" for a non-existing key
in following code:
c.execute("SELECT * FROM table1 where col1 = 'test'")
res = c.fetchall()
sum = 0
for x in res:
print "res: ",res
d = {"num1" : [ str(testVal[x[2]]['num1']) for x in res ],
"num2" : [ str(testVal[x[2]]['num2']) for x in res ],
}
conn.close()
This is the error:
"num1": [ str(testVal[x[2]]['num1']) for x in res ],
KeyError: u'13'
How can I check to see if that key has value then assign it to "num1", "num2"
.