收到错误消息:列表索引超出范围

时间:2019-05-01 22:00:20

标签: python-3.x list csv

运行我的代码的这一部分时,出现上述错误,csv文件每行具有3个值,首先是名称,然后是ID号,最后是数字分数。我不知道为什么说列表索引超出范围。

当我将for循环内的代码更改为仅打印行时,它会将其打印出来,就好像它是具有3个值的列表一样。

csv文件如下

sean,12,15

harry,132,12

ben,3322,11

等,

我仔细检查了文件,看是否每个行中没有3个值,但事实并非如此。每行正好有3个值,以2个逗号分隔。

 points=[]
 names = []
 ids = []
 quiztakers = 0 
 totalscore = 0 
 with open('scoreFilecsv','r+') as score:
     reader = csv.reader(score,delimiter=',')
     for row in score:

         point = row[2]
         totalscore += point
         quiztakers += 1
         name = row[0]
         ids1 = row[1]
         points.append(point)
         names.append(name)
         ids.append(ids1)

1 个答案:

答案 0 :(得分:0)

  • 下面的代码是我尝试过的并且有效

    points=[]
    names = []
    ids = []
    quiztakers = 0 
    totalscore = 0
    i = 0
    score = ["sean",12,15, "harry",132,12]
    new_row = []
    for row in score:
    
        if i != 0 and i%3 == 0:
            point = new_row[2] 
            totalscore += point
            quiztakers += 1
            name = new_row[0]  
            ids1 = new_row[1]  
            points.append(point)
            names.append(name)
            ids.append(ids1)
            new_row = []
        new_row.append(row)
        i += 1
    else:
        point = new_row[2] 
        totalscore += point
        quiztakers += 1
        name = new_row[0]  
        ids1 = new_row[1]  
        points.append(point)
        names.append(name)
        ids.append(ids1)
        new_row = []
    
    print(names)
    print(points)
    print(ids)
    
  • 输出:

    ['sean', 'harry']
    [15, 12]
    [12, 132]
    
  • 查看您的代码,我相信row列表是因为row列表的长度在某些时候会确定小于2

  • 我认为您想在此处成为reader变量而不是score变量(我假设正在查看您的代码,但我不确定,因为我不知道您的代码是什么输入在EXCEL中是同一行还是不同行?)

     points=[]
     names = []
     ids = []
     quiztakers = 0 
     totalscore = 0 
     with open('scoreFilecsv','r+') as score:
    
         reader = csv.reader(score,delimiter=',')
         for row in score: # (Changing this line) for row in reader
    
             point = row[2] # you may be getting error here
             totalscore += point
             quiztakers += 1
             name = row[0]  # you may be getting error here
             ids1 = row[1]  # you may be getting error here
             points.append(point)
             names.append(name)
             ids.append(ids1)
    
  • 但是看看您对问题输入的描述,这就是我要做的

     points=[]
     names = []
     ids = []
     quiztakers = 0 
     totalscore = 0
     i = 0
     with open('scoreFilecsv','r+') as score:
         reader = csv.reader(score,delimiter=',')
         new_row = []
         for row in reader:
    
             if i != 0 and i%3 == 0:
                 point = new_row[2] 
                 totalscore += point
                 quiztakers += 1
                 name = new_row[0]  
                 ids1 = new_row[1]  
                 points.append(point)
                 names.append(name)
                 ids.append(ids1)
                 new_row = []
             new_row.append(row)
             i += 1
        else:
            point = new_row[2] 
            totalscore += point
            quiztakers += 1
            name = new_row[0]  
            ids1 = new_row[1]  
            points.append(point)
            names.append(name)
            ids.append(ids1)
            new_row = []
    
  • 自从您更新问题和输入内容以来:我将使用以下代码

     points=[]
     names = []
     ids = []
     quiztakers = 0 
     totalscore = 0 
     with open('scoreFilecsv','r+') as score:
    
         reader = csv.reader(score,delimiter=',')
         for row in reader: # (Changing this line) for row in reader
             row = row.split(",")
             point = row[2] 
             totalscore += point
             quiztakers += 1
             name = row[0]  
             ids1 = row[1]  
             points.append(point)
             names.append(name)
             ids.append(ids1)