TypeError: 元组的索引必须是整数,而不是字符串。

10 浏览
0 Comments

TypeError: 元组的索引必须是整数,而不是字符串。

我正在尝试从数据库中提取数据并将其分配给不同的列表。

这个特定的错误给我带来了很多麻烦,"TypeError: tuple indices must be integers, not str"。

我尝试将其转换为浮点数等,但没有成功。

代码如下所示:

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 or slices, not str

我该如何解决这个错误?

0