python - TypeError : tuple indices must be integers, not str

标签 python sql database

我正在尝试从数据库中提取数据并将它们分配给不同的列表。 这个特定的错误给我带来了很多麻烦“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/

相关文章:

python - 如何在Django中将明文密码转换为PBKDF2算法格式?

mysql - 从 "data"文件夹mysql转储数据

java - 尝试使用 JDBC 连接到数据库时应用程序崩溃

python - 如何调用为不同子组的列表组编写的函数

mysql - 使用 WHERE IS NULL 和 LIMIT 执行更新时可能发生冲突?

c# - C# 中的日期时间与 SQL Server 中的 SQL 和 GETDATE()

sqlite 匹配数字

c# - Start.Process 不在单独的进程中运行脚本

Python3+Django1.10+mysqlclient1.3.9 : cannot save emoji characters

python - 在 Windows 中使用 2to3 python