简体   繁体   English

python 列表中的 IndexError - “列表索引超出范围”

[英]IndexError in a python list - “list index out of range”

I'm trying to get certain data in columns from my MySQL database but I get this error when trying to index a list in a for loop.我正在尝试从我的 MySQL 数据库中获取列中的某些数据,但是在尝试在 for 循环中索引列表时出现此错误。 Everything works well until I'm trying to print out data from 1 column.在我尝试从 1 列打印数据之前,一切正常。

Here's the list.这是清单。 I do believe this is a tuple but that shouldn't be the problem.我确实相信这是一个元组,但这不应该是问题。 (I think) (我认为)

(1, 'Router', '192.168.1.1', '80')

Here's my code:这是我的代码:

myresult = mycursor.fetchall()
            for x in myresult:
                print(x)
                time.sleep(0.2)
            ip = myresult[2]
            print(ip)

This is the IndexError:这是索引错误:

.....
File "directory..", line 172, in huvudmeny2
    ip = myresult[2]
IndexError: list index out of range

Process finished with exit code 1

How can it be out of range when it has 4 items?当它有 4 个项目时,它怎么会超出范围?

The fetchall method returns a list of the remaining rows, each a tuple. fetchall方法返回剩余行的列表,每行都是一个元组。 It does not return the tuple.它不返回元组。

Hence there's a good chance you got less than three rows.因此,您很有可能获得的行数少于三行。

If you want the IP address of the first row from the query (for example), that would be:如果您想要查询中第一行的 IP 地址(例如),那将是:

myresult[0][2]

The following transcript shows this in action (after dummying up some data):以下成绩单显示了这一点(在虚拟化了一些数据之后):

>>> myresult = [(1, 'Router', '192.168.1.1', '80')]

>>> print(myresult)
[(1, 'Router', '192.168.1.1', '80')]

>>> print(myresult[2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

>>> print(myresult[0][2])
192.168.1.1

An alternative, if you know there will only be one row of useful data, is to use fetchone instead of fetchall .如果您知道只有一行有用数据,另一种方法是使用fetchone而不是fetchall This actually returns a single tuple rather than a list of tuples, so you can use your original indexing method.这实际上返回单个元组而不是元组列表,因此您可以使用原始索引方法。 You just need to check for None first since that's what you'll get if there were no rows returned.您只需要先检查None ,因为如果没有返回任何行,您将得到这样的结果。

But you already have that problem since you're not checking for an empty list in your original code, which is what fetchall gives you when there are no rows.但是您已经遇到了这个问题,因为您没有在原始代码中检查空列表,这是fetchall在没有行时为您提供的。

fetchall() returns a list, so you are printing index 2 of this list. fetchall()返回一个列表,因此您正在打印该列表的索引 2。

maybe you want something like:也许你想要类似的东西:

for x in myresult:
  print(x)
  time.sleep(0.2)
ip = myresult[0][2]
print(ip)

So you will access the 1st element of the list of tuples, then access the element you want.因此,您将访问元组列表的第一个元素,然后访问所需的元素。

You also can put the last 2 lines in the for loop to make sure they exist, like so:您还可以将最后两行放在 for 循环中以确保它们存在,如下所示:

for x in myresult:
  print(x)
  time.sleep(0.2)
  ip = myresult[2]
  print(ip)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM