我创建了以下类:
SchoolId SchoolYear TermCode TermBegin TermEnd TermCodeId
8CFD1 2016 TM1 08/29/2016 10/07/2016 F375E7
8CFD1 2016 TM2 10/10/2016 11/25/2016 898EF5
8CFD1 2016 TM3 11/28/2016 02/01/2017 F5140C
8CFD1 2016 TM4 02/02/2017 03/10/2017 DD95B1
8CFD1 2016 TM5 03/13/2017 05/05/2017 BE3635
8CFD1 2016 TM6 05/08/2017 06/09/2017 2B94F0
8CFD1 2016 YR 08/29/2016 10/07/2016 d97C9C
8CFD1 2016 YR 10/10/2016 11/25/2016 e30980
8CFD1 2016 YR 11/28/2016 02/01/2017 ef5de2
8CFD1 2016 YR 02/02/2017 03/10/2017 bbe78f
8CFD1 2016 YR 03/13/2017 05/05/2017 ca28a7
8CFD1 2016 YR 05/08/2017 06/09/2017 2340c1
当我在以下函数中调用此类时:
class Student:
def __init__(self, name, id, email):
self.__name = name
self.__id = id
self.__email_id = email
self.__marks = []
#accessors
def get_name(self):
return self.__name
def get_email(self):
return self.__email_id
def get_id(self):
return self.__id
def get_marks(self):
return self.__marks
#mututators
def set_name(self, name):
self.__name = name
def set_id(self, id):
self.__id = id
def set_email(self, email):
self.__email_id = email
def set__marks(self, marks):
self.__marks = marks
#formatted string representation of the student
def __str__(self):
return "%s: %s, %s, marks: %s" % (self.__id, self.__name, self.__email_id, self.__marks)
#appends the marks to the end of the marks list
def append_marks(self, marks):
self.__marks.append(marks)
它没有正确打印文本。出于某种原因,除了最后一个学生之外,它会混淆一切的格式。
示例:
def read_classlist():
#This function reads the classlist from file.
global studentslist
studentslist = []
try:
file=input("Enter name of the classlist file: ")
with open(file) as f:
for line in f:
data=line.split(",")
s=Student(data[0],data[1],data[2])
studentslist.append(s)
print("Completed reading of file %s" % file)
for student in studentslist:
print(student)
display_separator()
menu()
return
except IOError:
print("File %s could not be opened" % file)
display_separator()
menu()
return
我无法弄清楚为什么它会在电子邮件后创建一个新行。它为每个数据文件执行此操作,但显示的最后一个学生将始终是正确的。
我不太明白。
任何帮助都会很棒!
答案 0 :(得分:1)
我认为你应该改变这一行
data=line.split(",")
到
data=line.rstrip().split(",")
这将从您的数据中删除返回到新行字符\n
,并且您将获得所需的输出。