[英]Expected object or value error while reading json file in python
[英]Python creating an Object while reading file
我編寫了一個簡單的Python腳本來確定是否報告了所有學生的成績。 該腳本首先循環瀏覽,並將學生添加到有關年級狀態的數組中。 然后,我再次遍歷該文件,以確定每個學生的成績是否合格。我最終得到三個數組,其中包括“已報告所有成績”,“已報告某些成績”,“未報告任何成績”的學生。 但是,我想通過更多的面向對象方法來解決這個問題。 我試圖創建一個有效的類。 我堅持如何遍歷並為每個學生創建一個對象,然后使用添加課程將每門課程推入對象。 我可以成為一名更好的程序員的任何幫助都是很棒的!
數據:
**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
工作代碼:
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)
嘗試上課:
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))
您可以按照@blade的建議執行此操作,方法是創建一個按學生ID索引的字典,然后為輸入文件的每一行從字典中獲取現有學生(如果存在)或創建一個新學生。 在代碼中,這看起來像:
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
需要注意的幾件事。 首先,我修改了Student
類的構造函數,刪除了term參數,因為尚不清楚該term
在輸入文件中的指定位置。 此外,由於您不使用courses
參數,因此我也刪除了該參數。 (請注意,您可能不想使用[]
作為默認參數。 在此處了解可變的默認參數 。)您也不需要在addcourse
函數中為課程,成績和模式創建實例變量,您可以可以直接將它們附加到數組。
我還添加了一個調用,以strip
從輸入文件中提取的每個項目,以清除每行末尾的換行符。
這個怎么樣:
除了@JCVanHanne的答案外,您還可以在班級中定義另一個函數來收集信息,無論學生是否擁有,全部或全部成績都沒有。
一種可能的方式(假設缺少的成績由一個空字符串表示,而諸如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')
您可能會使用狀態碼或其他方式(例如返回值以進行進一步處理)來處理信息,而不僅僅是打印它們。 以打印命令為例:
students['10003'].gradeStatus() # leads to: No grades at all
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.