zl程序教程

您现在的位置是:首页 >  其它

当前栏目

TypeError: tuple indices must be integers or slices, not str

not or be must Str TypeError tuple Integers
2023-09-27 14:27:32 时间

已解决TypeError: tuple indices must be integers or slices, not str

在这里插入图片描述

报错问题

之前在工作中遇到过这个坑,记录一下问题以及解决方法,不一定针对所有情况都能用,但是可以供大家参考。
问题描述如下:
TypeError: tuple indices must be integers or slices, not str

for row in cur:  #每行规范输出
     print("name=%s, AGE=%d" % (row['name'], row['age']))

类型错误:元组索引必须是整数或切片,而不是字符串。

在这里插入图片描述

解决方法

在这里插入图片描述
可知cur此时是个tuple或者其他空列表等,而不是dict,只需要将其转为dict格式即可!

PS

有问题评论区留言即可