【问题标题】:IndexError: tuple index out of range ----- PythonIndexError:元组索引超出范围 ----- Python
【发布时间】:2013-12-16 06:22:47
【问题描述】:

请帮助我。我正在运行一个简单的 python 程序,它将以 tkinter 形式显示来自 mySQL 数据库的数据...

from Tkinter import *
import MySQLdb

def button_click():
    root.destroy()

root = Tk()
root.geometry("600x500+10+10")
root.title("Ariba")

myContainer = Frame(root)
myContainer.pack(side=TOP, expand=YES, fill=BOTH)

db = MySQLdb.connect ("localhost","root","","chocoholics")
s = "Select * from member"
cursor = db.cursor()
cursor.execute(s)
rows = cursor.fetchall()

x = rows[1][1] + " " + rows[1][2]
myLabel1 = Label(myContainer, text = x)
y = rows[2][1] + " " + rows[2][2]
myLabel2 = Label(myContainer, text = y)
btn = Button(myContainer, text = "Quit", command=button_click, height=1, width=6)

myLabel1.pack(side=TOP, expand=NO, fill=BOTH)
myLabel2.pack(side=TOP, expand=NO, fill=BOTH)
btn.pack(side=TOP, expand=YES, fill=NONE)

这就是整个程序......

错误是

x = rows[1][1] + " " + rows[1][2]
IndexError: tuple index out of range

y = rows[2][1] + " " + rows[2][2]
IndexError: tuple index out of range

谁能帮帮我???我是python新手。

非常感谢……

【问题讨论】:

  • 表示你访问的索引(位置)不存在。
  • 我将用什么代码替换我现有的代码???谢谢
  • 我不知道;你能提供一个small self-contained reproducible example 并编辑你的问题以包含它吗?
  • 您应该尝试使用 print(rows) 和 print(rows[1]) 来查看您的数据是什么样的。它可以帮助您找到问题。
  • 当我将 train_sizes=np.linspace(0.1, 1.0, 10) 更改为 train_sizes=10 时出现此错误

标签: python python-2.7 mysql-python


【解决方案1】:

我收到与
query = "INSERT INTO table(field1, field2,...) VALUES (%s,%s,...)" 相同的错误
但在声明中
cursor.execute(query, (field1, field2,..)
我根据需要提供了更少的变量...
在这种情况下,我使用了

import mysql.connector as mysql 

我只是想说这也是可能的......不仅在数组中
(我没有仔细研究这个具体案例......)

【讨论】:

    【解决方案2】:

    可能其中一个索引是错误的,无论是内部索引还是外部索引。

    我怀疑你的意思是说[0],你说的是[1],和[1],你说的是[2]。 Python 中的索引从 0 开始。

    【讨论】:

    • 我收到这条消息是因为我将一个数组传递给一个期望可变参数序列的函数(例如'{}{}'.format([1,2]) vs '{}{}'.format(*[1,2])
    • 另一种情况是字符串中有两个“{}”,而 .format() 中只有一个变量/值
    • 如果您在参数列表中有一个放错位置的赋值运算符 (=),这是另一个原因。我已经改组了一些代码并结束了。
    • ...作为 Dror 评论的变体:在格式字符串中包含 {} 而不是 {{}}。当有人想要从字面上打印一对花括号时,这是一个典型的复制粘贴错误。
    • 我也遇到同样的错误,你能查一下stackoverflow.com/questions/46945860/…
    【解决方案3】:

    格式化函数

    此错误可能发生在 format() 函数行中。 在你的机器上试试下面的代码

    >>> 'This is just a string {}'.format()

    请注意发生此错误。但是,因为使用了尖括号,所以没有 参数被传递给format()。在 Python 术语中,

    参数的数量必须与花括号的数量匹配或更高。

    在下面几个例子不被认为是好的做法,但就 Python3 而言可能没有错误。

    >>> 'This is just a string'.format()  # No curly braces no arguments. Match in proportion.
    >>> 'This is just a string {}'.format('WYSIWYG', 'J. Pinkman', 'ESR')  # The number of arguments is superior to the number of curlies( *curly braces* ).
    

    【讨论】:

      【解决方案4】:

      元组由许多用逗号分隔的值组成。喜欢

      >>> t = 12345, 54321, 'hello!'
      >>> t[0]
      12345
      

      元组在 Python 中是基于索引的(也是不可变的)。

      在这种情况下,x = rows[1][1] + " " + rows[1][2] 只有两个索引 0、1 可用,但您正在尝试访问第三个索引。

      【讨论】:

        【解决方案5】:

        这是因为您的 row 变量/元组不包含该索引的任何值。您可以尝试打印整个列表,例如 print(row),并检查存在多少索引。

        【讨论】:

        • 好建议...帮助了我。
        猜你喜欢
        • 2014-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-28
        • 2018-11-16
        • 2017-07-12
        相关资源
        最近更新 更多