Python 文本和行读取错误

Python Text And Row reading error

我目前正在作为 GCSE 学生学习计算机科学。我和我的老师正在努力完成任务 3。任务的要求是: 然后它需要询问用户 class 他们想查看什么
在此之后,它将需要询问他们是否想查看按字母顺序排序的文件,特定 class 的高分,或者他们是否想查看所选 class [中每个学生的平均分]

import csv
class_a = open('classA.txt')

csv_a = csv.reader(class_a)

for row in csv_a:
    row[2] = int(row[2])
print(row[2])

当我 运行 我的程序出现这个错误

 Traceback (most recent call last):
File "C:\Users\Roo\Desktop\Python\Class\Test.py", line 7, in <module>
row[1]= int(row[1])
IndexError: list index out of range

我的代码 python 中的 运行 告诉我第 13 行

有问题
"index out of range"

我的文本文件包含:

Roo,2,3,3  
Roo,4,4,5  
Alfie,5,8,2    
Alfie,2,8,5    
Bob,2,8,6    
Connor,3,5,9    
Connor,5,3,3  
Ellis,5,6,2  
George,5,4,1   
Ellis,4,9,3   
Nathan,5,6,5   
George,5,5,6    
Alfie,9,4,7    
George,4,7,9    
Celis,4,5,4    
Leo,3,2,3    
Celis,6,1,2  
Leo,5,2,1 

当我运行程序代码时,row[2] = int(row[2])告诉我它超出了范围?任何解决方案?

请保持基本,我目前是 python 的新人,不理解所有技术术语

您可能有一个空行 classA.txt.

结束

使用

for row in csv_a:
    if row:              # or better if len(row)==4:
          row[2] = int(row[2])
          print(row[2])

检查该行是否不为空,之前 您访问 second/third 条目。