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.
我试图从数据库中提取数据并将它们分配给不同的列表。这个特定的错误给了我很多麻烦“TypeError:元组索引必须是整数,而不是str”我试着将它转换为float等,但没有成功。
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 :
print语句的示例输出如下:
('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 !
任何帮助,将不胜感激 !谢谢大家!
3 个解决方案
#1
8
Like the error says, row
is a tuple, so you can't do row["pool_number"]
. You need to use the index: row[0]
.
就像错误说的那样,row是一个元组,所以你不能做row [“pool_number”]。您需要使用索引:row [0]。
#2
1
The Problem is how you access row
问题是你如何访问行
Specifically row["waocs"]
and row["pool_number"]
of ocs[row["pool_number"]]=int(row["waocs"])
特别是ocs的row [“waocs”]和row [“pool_number”] [row [“pool_number”]] = int(row [“waocs”])
If you look up the official-documentation of 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]
因此,您必须访问行[__ integer__]的行的值,如row [0]
#3
1
TL;DR: add the parameter cursorclass=MySQLdb.cursors.DictCursor
at the end of your MySQLdb.connect
.
TL; DR:在MySQLdb.connect的末尾添加参数cursorclass = MySQLdb.cursors.DictCursor。
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.DictCursor
at 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.
因此,作为已经提供的解决方案的替代方案,您还可以添加此参数并以您最初想要的方式访问行。 ^ _ ^我希望这有助于其他人。
#1
8
Like the error says, row
is a tuple, so you can't do row["pool_number"]
. You need to use the index: row[0]
.
就像错误说的那样,row是一个元组,所以你不能做row [“pool_number”]。您需要使用索引:row [0]。
#2
1
The Problem is how you access row
问题是你如何访问行
Specifically row["waocs"]
and row["pool_number"]
of ocs[row["pool_number"]]=int(row["waocs"])
特别是ocs的row [“waocs”]和row [“pool_number”] [row [“pool_number”]] = int(row [“waocs”])
If you look up the official-documentation of 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]
因此,您必须访问行[__ integer__]的行的值,如row [0]
#3
1
TL;DR: add the parameter cursorclass=MySQLdb.cursors.DictCursor
at the end of your MySQLdb.connect
.
TL; DR:在MySQLdb.connect的末尾添加参数cursorclass = MySQLdb.cursors.DictCursor。
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.DictCursor
at 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.
因此,作为已经提供的解决方案的替代方案,您还可以添加此参数并以您最初想要的方式访问行。 ^ _ ^我希望这有助于其他人。