python - Remove the newline character in a list read from a file -


i have simple program takes id number , prints information person matching id. information stored in .dat file, 1 id number per line.

the problem program reading newline character \n file. have tried 'name'.split() method, doesn't seem work list.

my program:

from time import localtime, strftime  files = open("grades.dat") request = open("requests.dat", "w") lists = files.readlines() grades = []  in range(len(lists)):     grades.append(lists[i].split(","))  cont = "y"  while cont == "y" or cont == "y":     answer = raw_input("please enter student i.d. of whom looking: ")     in range(len(grades)):         if answer == grades[i][0]:             print grades[i][1] + ", " + grades[i][2] + (" "*6) + grades[i][0] + (" "*6) + grades[i][3]             time = strftime("%a, %b %d %y %h:%m:%s", localtime())             print time             print "exams - " + grades[i][11] + ", " + grades[i][12] + ", " + grades[i][13]             print "homework - " + grades[i][4] + ", " + grades[i][5] + ", " + grades[i][6] + ", " + grades[i][7] + ", " +grades[i][8] + ", " + grades[i][9] + ", " + grades[i][10]             total = int(grades[i][4]) + int(grades[i][5]) + int(grades[i][6]) + int(grades[i][7]) + int(grades[i][8]) + int(grades[i][9]) + int(grades[i][10]) + int(grades[i][11]) + int(grades[i][12]) + int(grades[i][13])             print "total points earned - " + str(total)             grade = float(total) / 550             grade = grade * 100             if grade >= 90:                 print "grade: " + str(grade) + ", equal a."             elif grade >= 80 , grade < 90:                 print "grade: " + str('%.2f' %grade) + ", equal b."             elif grade >= 70 , grade < 80:                 print "grade: " + str('%.2f' %grade) + ", equal c."             elif grade >= 60 , grade < 70:                 print "grade: " + str('%.2f' %grade) + ", equal d."             else:                 print "grade: " + str('%.2f' %grade) + ", equal f."             request.write(grades[i][0] + " " + grades[i][1] + ", " + grades [i][2] +                           " " + time)             request.write("\n")       print     cont = raw_input("would search again? ")  if cont != "y" or cont != "y":     print "goodbye." 

str.strip() returns string leading+trailing whitespace removed, .lstrip , .rstrip leading , trailing respectively.

grades.append(lists[i].rstrip('\n').split(',')) 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -