TypeError:元组索引必须是整数,而不是str [英] TypeError: tuple indices must be integers, not str

查看:58
本文介绍了TypeError:元组索引必须是整数,而不是str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库中提取数据并将其分配给其他列表.这个特定的错误给我带来了很多麻烦"TypeError:元组索引必须是整数,而不是str"我尝试将其转换为float等,但没有成功.

I am trying to pull data from a database and assign them to different lists. This specific error is giving me a lot of trouble "TypeError: tuple indices must be integers, not str" I tried converting it to float and etc, but to no success.

代码如下

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"])

print语句的示例输出如下:

Sample output of print statement is as follows :

('MA3146', 711L, 81L)
('MA3147', 679L, 83L)
('MA3148', 668L, 86L)

这是我得到的确切错误:

And this is the exact error I am getting:

ocs[row["pool_number"]]=int(row["waocs"])
TypeError: tuple indices must be integers, not str

任何帮助将不胜感激!谢谢大家!

Any help would be appreciated! Thanks people!

推荐答案

就像错误说的那样, row 是一个元组,因此您不能执行 row ["pool_number"] .您需要使用索引: row [0] .

Like the error says, row is a tuple, so you can't do row["pool_number"]. You need to use the index: row[0].

这篇关于TypeError:元组索引必须是整数,而不是str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆