0

when running this portion of my code I get the above error, the csv file has 3 values per row, first a name, then id number, lastly a numerical score. I can not figure out why it is saying the list index is out of range.

when I change the code inside the for loop to just print row it prints it out as if it was a list with 3 values.

the csv file looks like this

sean,12,15

harry,132,12

ben,3322,11

etc,

I've looked through the file to see if any rows did not have 3 values in each one but that was not the case. Each line has exactly 3 values seperated by 2 commas.

 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)
Archer15
  • 1
  • 1
  • we also tried changing score to reader but the same issue arose in line 7 – Archer15 May 01 '19 at 22:02
  • 1
    There is exactly one reason for getting this error, which is using an index that is beyond the beginning or end of the list or array or collection, which means that one of the lines in your file does not contain what you think it does. We can't see your file or step through your code for you, so you'll need to do that yourself. – Ken White May 01 '19 at 22:03
  • 2
    Possible duplicate of [Index Error: list index out of range (Python)](https://stackoverflow.com/questions/16005707/index-error-list-index-out-of-range-python) – Ken White May 01 '19 at 22:05
  • @KenWhite i checked our csv file and all lines have 3 values so row[2] should be a valid index each iteration. – Archer15 May 01 '19 at 22:15
  • 1
    You're mistaken. There is exactly one reason to get this error. Please read my previous comment again (and the duplicate I linked above). – Ken White May 01 '19 at 22:18

1 Answers1

0
  • Below code is what I tried and it works

    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)
    
  • Output:

    ['sean', 'harry']
    [15, 12]
    [12, 132]
    
  • Looking at your code I believe in the row list because the length of row list at some point will be definitely less then 2

  • I think you wanted to be reader variable over there instead of score variable (I am assuming looking at your code, but I am not sure, since I do not know what your input is in EXCEL is it in same row or different rows?)

     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)
    
  • But looking at your description of input in the question, this is what I would do

     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 = []
    
  • Since you updated your question and input: I will go with the below code

     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)
    
Jai
  • 3,211
  • 2
  • 17
  • 26
  • You are welcome @Archer15 ... Can you please mark this answer as correct and give upvote – Jai May 01 '19 at 22:54