python - AttributeError: 'list' object has no attribute 'readline'? -


def read_grade(gradefile):      '''     (file,'r') --->  list of floats      in case return grade file,        fution meant use given grade file'''      line = gradefile.readline()     print(line)      while line != '\n':        line = gradefile.readline()        print(line)      grades = []     line = gradefile.readline()    print(line)      while line != '':        grade = line[line.rfind(' ') + 1:]        grades.append(float(grade))        line = gradefile.readline()        print(line)      return grades  def range_grade(grades):             '''     (list of int) ---> list of int      return list each index indicates how many grades in these ranges    '''    grade_range = [0] *11     grade in grades:       which_range = int(grade // 10)       grade_range[which_range] = grade_range[which_range] + 1     return grade_range    #main body starts    file_path = 'c:/users/user_name/desktop/python/grade.txt'  file = open(file_path,'r')   hista_file_path ='c:/users/user_name/desktop/python/hista.txt'  hista_file = open(hista_file_path,'w')   #read grades  grades = read_grade(file)  #count grade per range  range_counts = read_grade(grades)  print(range_counts) 

prints before main body debugging, problem while loop not stopping when line == '', eof

grade.txt

*grade file python learning created mukul jain on 11 may 2015  0052 77.5 0072 66 0133 100 0123 89 0402 51 0032 72 0144 22 0082 26 0024 79 0145 12 0524 60 0169 99 

the file grade.txt ends after \n "99" i.e. 99\n

this question python tut on coursera

any grateful

if want second column in file floats:

def read_grade(gradefile):     next(gradefile)     next(gradefile)     return [float(line.split()[1]) line in gradefile] 

this assumes want throw away first 2 lines in file. split each line @ whitespace , convert numbers in second column floats.

now:

grades = read_grade(gradefile) range_grade(grades) 

returns:

[0, 1, 2, 0, 0, 1, 2, 3, 1, 1, 1] 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -