I am getting error ..list out of range with this code.. can someone help pls?

def classGrade(infile):
    inf=open(infile)
    contents=inf.readlines()
    inf.close()

    classg={}
    for line in contents:
        y=line.split("\t")
        d={}
        print(y)

        d['id']=y[0]
        d['exam1']=y[1]
        d['exam2']=y[2]
        d['homework']=y[3]
        d['attendance']=y[4]
        d['project']=y[5]
        d['classrecap']=y[6]

        classg[y[0]]=d
    print(classg)
print (classGrade('mock1.txt'))
share|improve this question
2  
Looks like mock1.txt is bad. – hyades 36 mins ago
2  
Add a print(y) after the split. Bet there's a blank line. – kindall 32 mins ago
    
You are going to have to re-check the contents of mock1.txt. If possible, you may show a sample of its contents to us (by adding it to your question post). – Ébe Isaac 32 mins ago
    
Looks like you're splitting on tabs (\t) -- Are you sure that those are tabs and not spaces? (Your text editor may use spaces for the tab key) – sytech 17 mins ago
    
please update the txt file or give a sample – 宏杰李 5 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.