我在以下代码中为不存在的KeyError
获取了“key
”:
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()
这是错误:
"num1": [ str(testVal[x[2]]['num1']) for x in res ],
KeyError: u'13'
如何查看该密钥是否有价值,然后将其分配给"num1", "num2"
。