if class_number == ('1') and sort_by == ('a') or ('A'):
    csv_file = open('Class1_Test_Score.csv', 'a')
    csv_file.write('\n')
    csv_file.write(sname + ' ' + fname)
    csv_file.write(',')
    csv_file.write(score)
    csv_file.close()
    sort1 = open('Class1_Test_Score.csv', 'r')
    sorting = csv.reader(sort1, delimiter = ',')
    sort = sorted(sorting,key=operator.itemgetter(1))
    for eachline in sort:
        csv_file.write(eachline)
            Asked
            
        
        
            Active
            
        
            Viewed 54 times
        
    2
            
            
         
    
    
        mechanical_meat
        
- 163,903
- 24
- 228
- 223
 
    
    
        Ross Martin
        
- 21
- 2
- 
                    2You're closing your file before trying to write to it. – mechanical_meat Feb 11 '16 at 23:53
- 
                    I've changed the code to as follows: if class_number == ('1') and sort_by == ('a') or ('A'): csv_file = open('Class1_Test_Score.csv', 'a') csv_file.write('\n') csv_file.write(sname + ' ' + fname) csv_file.write(',') csv_file.write(score) sort1 = open('Class1_Test_Score.csv', 'r') sorting = csv.reader(sort1, delimiter = ',') sort = sorted(sorting,key=operator.itemgetter(0)) for eachline in sort: csv_file.write(eachline) csv_file.close() – Ross Martin Feb 12 '16 at 00:05
- 
                    But i still receive this error message: sort = sorted(sorting,key=operator.itemgetter(0)) IndexError: list index out of range – Ross Martin Feb 12 '16 at 00:07
1 Answers
0
            
            
        Is every row in your Nested List its own list object with length greater than 0 ?
The majority of your rows are likely list objects with multiple items, giving it a :
    len ( row ) > 1 
But I'm guessing there may be at least one Row in your Nested List that has a length of 0. In other words whereas most of your rows look like this :
    [  'First Item' ,  2  ,  "3rd Item"   ]
There may be one Row that looks like this :
    [   ]
And you cannot sort with operator.itemgetter unless every row in that nested list has an item at index number 0 ( or another column number you choose to input in your itemgetter ).
 
    
    
        RasikhJ
        
- 601
- 5
- 10