我正在尝试从数据库中提取数据并将它们分配给不同的列表。 这个特定的错误给我带来了很多麻烦“TypeError:元组索引必须是整数,而不是str” 我尝试将其转换为 float 等,但没有成功。
代码如下
conn=MySQLdb.connect(*details*)
cursor=conn.cursor()
ocs={}
oltv={}
query="select pool_number, average_credit_score as waocs, average_original_ltv as waoltv from *tablename* where as_of_date= *date*"
cursor.execute(query)
result=cursor.fetchall()
for row in result:
print row
ocs[row["pool_number"]]=int(row["waocs"])
oltv[row["pool_number"]]=int(row["waoltv"])
打印语句的示例输出如下:
('MA3146', 711L, 81L)
('MA3147', 679L, 83L)
('MA3148', 668L, 86L)
这是我得到的确切错误:
ocs[row["pool_number"]]=int(row["waocs"])
TypeError: tuple indices must be integers, not str
任何帮助将不胜感激!谢谢大家!
最佳答案
就像错误所说,row
是一个元组,所以你不能做 row["pool_number"]
。您需要使用索引:row[0]
.
关于python - TypeError : tuple indices must be integers, not str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35359969/