[英]Python creating an Object while reading file
I wrote a simple Python script to determine if all students grades are reported. 我编写了一个简单的Python脚本来确定是否报告了所有学生的成绩。 The script first loops through and adds students to arrays regarding grade status.
该脚本首先循环浏览,并将学生添加到有关年级状态的数组中。 Then I loop through the file again, to determine if each students grades are in. I end up with three arrays that include students with "all grades reported", "some grades reported", "no grades reported".
然后,我再次遍历该文件,以确定每个学生的成绩是否合格。我最终得到三个数组,其中包括“已报告所有成绩”,“已报告某些成绩”,“未报告任何成绩”的学生。 However, I want tackle this problem with more of a object oriented approach.
但是,我想通过更多的面向对象方法来解决这个问题。 I have attempted to create a class that works.
我试图创建一个有效的类。 I am stuck at how to loop through and create one Object for each student, then use addcourse to push each course into the Object.
我坚持如何遍历并为每个学生创建一个对象,然后使用添加课程将每门课程推入对象。 Any help I can get to become a better programmer would be great!
我可以成为一名更好的程序员的任何帮助都是很棒的!
Data: 数据:
**id,fname,lname,course,grade,mode**
10001,Freddy,Freshman,Art-101,A,online
10001,Freddy,Freshman,Art-101,A,online
10002,Suize,Sophmore,Mat-102,C,inperson
10002,Suize,Sophmore,Bio-101, ,inperson
10002,Suize,Sophmore,Soc-201,D,online
10003,Jilly,Junior,mth-102, ,inperson
10003,Jilly,Junior,Bus-101, ,inperson
10003,Jilly,Junior,Che-204, ,inperson
Working Code: 工作代码:
fh = open('students.txt').readlines()
header = fh.pop(0)
gradereported = []
nogradereported = []
for line in fh:
students = line.split(',')
ids = students[0]
grade = students[4]
if grade != "":
gradereported.append(ids)
else:
nogradereported.append(ids)
allgradesin =[]
nogradesin = []
somegradesin = []
for i in fh:
students = line.split(',')
ids = students[0]
if ids in gradereported and ids not in nogradereported:
if ids not in allgradesin:
allgradesin.append(ids)
elif ids not in gradereported and ids in nogradereported:
if ids not in nogradesin:
nogradesin.append(ids)
elif ids in gradereportedand and ids in nogradereported:
if ids not in somegradesin:
somegradesin.append(ids)
Attempt at class: 尝试上课:
class Student(object):
def __init__(self, lname, fname, term, courses = []):
self.studid = studid
self.lname = lname
self.fname = fname
self.term = term
self.courses = []
def addcourse(self, course, grade, mode):
self.course = course
self.grade = grade
self.mode = mode
self.courses.append((self.course, self.grade, self.mode))
You could do this, as @blade suggests, by creating a dictionary indexed by student id and then for each row of your input file either get the existing student from the dictionary if it exists or create a new one. 您可以按照@blade的建议执行此操作,方法是创建一个按学生ID索引的字典,然后为输入文件的每一行从字典中获取现有学生(如果存在)或创建一个新学生。 In code, this would look like:
在代码中,这看起来像:
class Student(object):
def __init__(self, student_id, lname, fname):
self.studid = student_id
self.lname = lname
self.fname = fname
self.courses = []
def addcourse(self, course, grade, mode):
self.courses.append((course, grade, mode))
students = {}
fh = open('students.txt').readlines()
header = fh.pop(0)
for line in fh:
row = line.split(',')
if len(row) < 6:
continue
student_id, fname, lname, course, grade, mode = [i.strip() for i in row]
student = students.get(student_id, Student(student_id, lname, fname))
student.addcourse(course, grade, mode)
students[student_id] = student
A couple of things to note. 需要注意的几件事。 First, I modified the constructor of your
Student
class, dropping the term argument since it wasn't clear where the term
was specified in your input file. 首先,我修改了
Student
类的构造函数,删除了term参数,因为尚不清楚该term
在输入文件中的指定位置。 Furthermore, since you don't use the courses
argument I dropped that as well. 此外,由于您不使用
courses
参数,因此我也删除了该参数。 (Note that you probably don't want to use []
as a default argument. Read about mutable default arguments here .) You also don't need to create instance variables for the course, grade, and mode in your addcourse
function, you can just append them directly to the array. (请注意,您可能不想使用
[]
作为默认参数。 在此处了解可变的默认参数 。)您也不需要在addcourse
函数中为课程,成绩和模式创建实例变量,您可以可以直接将它们附加到数组。
I also added a call to strip
for each of the items pulled from the input file to clean up the newlines at the end of each row. 我还添加了一个调用,以
strip
从输入文件中提取的每个项目,以清除每行末尾的换行符。
How about this: 这个怎么样:
In addition to the answer of @JCVanHanne you could define another function in your class to collect the info, whether a student has none, some or all of his/her grades. 除了@JCVanHanne的答案外,您还可以在班级中定义另一个函数来收集信息,无论学生是否拥有,全部或全部成绩都没有。
One possible way (assuming a missing grade is represented by an empty string while also grades like A+ or other none-empty values are possible) could be: 一种可能的方式(假设缺少的成绩由一个空字符串表示,而诸如A +或其他非空值的成绩也是可能的)可能是:
def gradeStatus(self):
miss = [course[1] for course in self.courses].count("") # count empty grades
if len(self.courses) == miss:
print('No grades at all')
elif miss in range(1, len(self.courses)):
print('Some, but not all grades')
elif miss == 0:
print('All grades provided')
else:
print('Invalid Data')
You probably would use status codes or other ways (like a return value to further process) to work with the information than just printing them. 您可能会使用状态码或其他方式(例如返回值以进行进一步处理)来处理信息,而不仅仅是打印它们。 As an example with the print commands:
以打印命令为例:
students['10003'].gradeStatus() # leads to: No grades at all
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.