Python 类型错误:元组索引必须是整数,而不是 str

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35359969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:20:46  来源:igfitidea点击:

TypeError: tuple indices must be integers, not str

pythonsqldatabase

提问by Harsha Jasti

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.

我正在尝试从数据库中提取数据并将它们分配给不同的列表。这个特定的错误给我带来了很多麻烦“类型错误:元组索引必须是整数,而不是 str”我尝试将其转换为浮点数等,但没有成功。

The code goes as below

代码如下

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

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!

任何帮助,将不胜感激!谢谢人们!

采纳答案by Daniel Roseman

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

就像错误所说的那样,row是一个元组,所以你不能做row["pool_number"]. 您需要使用索引:row[0]

回答by Altoyyr

The Problem is how you access row

问题是你如何访问 row

Specifically row["waocs"]and row["pool_number"]of ocs[row["pool_number"]]=int(row["waocs"])

具体地说row["waocs"]row["pool_number"]ocs[row["pool_number"]]=int(row["waocs"])

If you look up the official-documentationof fetchall()you find.

如果您查找官方文档fetchall()您会发现。

The method fetches all (or all remaining) rows of a query result set and returns a list of tuples.

该方法获取查询结果集的所有(或所有剩余)行并返回元组列表。

Therefore you have to access the values of rows with row[__integer__]like row[0]

因此,您必须使用row[__integer__]类似的方式访问行的值row[0]

回答by msb

TL;DR: add the parameter cursorclass=MySQLdb.cursors.DictCursorat the end of your MySQLdb.connect.

TL;DR:cursorclass=MySQLdb.cursors.DictCursorMySQLdb.connect.



I had a working code and the DB moved, I had to change the host/user/pass. After this change, my code stopped working and I started getting this error. Upon closer inspection, I copy-pasted the connection string on a place that had an extra directive. The old code read like:

我有一个工作代码并且数据库移动了,我不得不更改主机/用户/密码。在此更改后,我的代码停止工作,我开始收到此错误。仔细检查后,我将连接字符串复制粘贴到一个有额外指令的地方。旧代码如下:

 conn = MySQLdb.connect(host="oldhost",
                        user="olduser",
                        passwd="oldpass",
                        db="olddb", 
                        cursorclass=MySQLdb.cursors.DictCursor)

Which was replaced by:

取而代之的是:

 conn = MySQLdb.connect(host="newhost",
                        user="newuser",
                        passwd="newpass",
                        db="newdb")

The parameter cursorclass=MySQLdb.cursors.DictCursorat the end was making python allow me to access the rows using the column names as index. But the poor copy-paste eliminated that, yielding the error.

最后的参数cursorclass=MySQLdb.cursors.DictCursor是让 python 允许我使用列名作为索引访问行。但是糟糕的复制粘贴消除了这一点,从而产生了错误。

So, as an alternative to the solutions already presented, you can also add this parameter and access the rows in the way you originally wanted. ^_^ I hope this helps others.

因此,作为已经提供的解决方案的替代方案,您还可以添加此参数并以您最初想要的方式访问行。^_^ 我希望这对其他人有帮助。

回答by Sitti Munirah Abdul Razak

I think you should do

我认为你应该做

for index, row in result: 

If you wanna access by name.

如果您想按名称访问。