简体   繁体   English

从CSV文件读取时索引超出范围

[英]Index out of range when reading from a csv file

this code reads data from a text file and then proceeds to sort the list created by the program by the number contained in each row (lowest to highest), then the program should match the name entered with one from from the list and then proceed to calculate that persons place. 此代码从文本文件中读取数据,然后继续按每行中包含的编号(从最低到最高)对程序创建的列表进行排序,然后程序应将输入的名称与列表中的名称相匹配,然后继续进行操作计算那个人的位置。

Here is the code: 这是代码:

def Rank():
  #Declare List
  RankList=[]
  #Opening and reading the file
  with open('GolfInfo.txt','rU') as csvfile:
        reader=csv.reader(csvfile)
        for row in reader:
              #Data added to list
              RankList.append(row)
  #List is sorted by amount of strokes taken (Total) from lowest to highest
  RankList.sort()
  index=0
  Position=0
  RankMessageFull=("")
  for row in RankList:
        #Checks to see if Name is present in the list
        if row[1] == Name.get():
            Position=index+1
            #These if-elif-else statements determine a suitable suffix for the rank
            if Position==1:
                  RankMessageFull=(Position,"st")
                  RankMessage.set(RankMessageFull)
            elif Position==2:
                  RankMessageFull=(Position,"nd")
                  RankMessage.set(RankMessageFull)
            elif Position==3:
                  RankMessageFull=(Position,"rd")
                  RankMessage.set(RankMessageFull)
            else:
                  RankMessageFull=(Position,"th")
                  RankMessage.set(RankMessageFull)

        else:
              index=index+1

The textfile reads: 文本文件显示为:

63,April,"('-', 7)"
69,Betsy,"('-', 1)"
80,Laura,"('+', 10)"
93,Coco,"('+', 23)"

The error occurs in the line: 该行中发生错误:

if row[1] == Name.get():

But I do not know why. 但是我不知道为什么。 The full error message can be seen here: 完整的错误消息可以在这里看到:

if row[1] == Name.get():
  IndexError: list index out of range

RankMessage is a Tkinter Stringvar , Name is also a Tkinter StringVar . RankMessage是Tkinter StringvarName也是Tkinter StringVar

It appears that there are lines in the csv file that have only one or fewer items on them. 在csv文件中似乎有些行仅包含一个或更少的项目。 You can avoid adding them to the RankList with the simple change shown below. 您可以通过以下所示的简单更改避免将它们添加到RankList中。 Also note you should open() csv files using the newline='' option in Python 3. 另请注意,您应该在Python 3中使用newline=''选项open() csv文件。

def Rank():
  #Declare List
  RankList=[]
  #Opening and reading the file
  with open('GolfInfo.txt','rU', newline='') as csvfile:
        reader=csv.reader(csvfile)
        for row in reader:
              #Data added to list if not blank.
              if row:  # ADDED TEST!
                    RankList.append(row)
  #List is sorted by amount of strokes taken (Total) from lowest to highest
  RankList.sort()
  ...

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

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