处理异常KeyError - Python

时间:2015-07-14 21:41:23

标签: python keyerror

我在以下代码中为不存在的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"

0 个答案:

没有答案