索引器错误:元组索引超出范围------Python

索引器错误:元组索引超出范围------Python,python,python-2.7,mysql-python,Python,Python 2.7,Mysql Python,请帮帮我。我正在运行一个简单的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, expa

请帮帮我。我正在运行一个简单的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新手


非常感谢……

可能有一个索引是错误的,要么是内部索引,要么是外部索引


我猜你的意思是说
[0]
在你说
[1]
的地方和
[1]
在你说
[2]
的地方。在Python中,索引是基于0的。

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

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

>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345
元组在Python中是基于索引的(也是不可变的)

在本例中,
x=行[1][1]+“”+行[1][2]
只有两个索引0,1可用,但您正在尝试访问第三个索引。

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

这只是一个字符串{}。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* ).

这表示您正在访问的索引(位置)不存在。我将用什么代码替换现有代码???谢谢你,我不知道;您是否可以提供一个列表并编辑您的问题以包含该列表。您应该尝试打印(行)和打印(行[1])以查看数据的外观。当我将train_size=np更改为train_size=10I时,它可能会帮助您找到问题。当我将train_size=np更改为train_size=10I时,我收到了此消息,因为我正在将数组传递给一个函数,该函数需要一个可变的参数序列(例如
'{}{}'。format([1,2])
vs
'{}{}'。format(*[1,2]))
另一种情况是,如果赋值运算符放错了位置(
=
),字符串中意外地有两个“{}”,而.format()中只有一个变量/值在一个参数列表中,这是导致这个问题的另一个原因。我将一些代码混在一起,并以它结束…作为Dror评论的变体:在格式字符串中包含
{}
,而不是
{}
。当有人想要打印一对大括号时,这是一个典型的复制粘贴错误。我也遇到了同样的错误,你能检查一下吗