2016-10-19 79 views
1

我需要使用具有5個變量的'Student'類並使用多個文件創建對象。Python使用多個文件從一個類創建對象

的文本文件:(Students.txt)

Last Name Midle Name First Name Student ID 
---------------------------------------------- 
Howard     Moe   howar1m  
Howard     Curly  howar1c  
Fine     Lary  fine1l  
Howard     Shemp  howar1s  
Besser     Joe   besse1j  
DeRita  Joe   Curly  derit1cj  
Tiure  Desilijic Jaba  tiure1jd  
Tharen     Bria  thare1b  
Tai   Besadii  Durga  tai1db  

文本文件2:(CourseEnrollment.txt)

PH03 
---- 
fine1l 
howar1s 
besse1j 
derit1cj 
tiure1jd 
targa1d 
bomba1t 
brand1m 
took1p 
mccoy1l 
solo1h 
edrie1m 
mccoy1e 
adama1l 
grays1z 

MT03 
---- 
cottl1s 
fine1l 
clega1s 
targa1d 
took1p 
mccoy1l 
crush1w 
dane1a 
monto1i 
rugen1t 
talto1v 
watso1j 
carpe1m 
rosli1l 
biggs1gj 
tigh1e 

PH05 
---- 
zarek1t 
adama1w 
tigh1s 
cottl1s 
howar1m 
howar1s 
besse1j 
balta1g 
derit1cj 
thare1b 
hego1d 
lanni1t 
stark1a 
clega1s 
scott1m 
monto1i 
flaum1e 
watso1j 
biggs1gj 
dane1a 

EN01 
---- 
howar1c 
fine1l 
tai1db 
targa1d 
brand1m 
corey1c 
edrie1m 
watso1j 
carpe1m 
sobch1w 

EN02 
---- 
howar1m 
howar1s 
besse1j 
tiure1jd 
tai1db 
hego1d 
lanni1t 
stark1a 
mccoy1l 
scott1m 
crush1w 
dane1a 
monto1i 
rugen1t 
solo1h 
flaum1e 
talto1v 
watso1j 
mccoy1e 

CS02 
---- 
howar1m 
howar1c 
besse1j 
derit1cj 
thare1b 
hego1d 
clega1s 
targa1d 
brand1m 
rugen1t 
flaum1e 
talto1v 
mccoy1e 
grube1h 

AR00 
---- 
tigh1e 
rosli1l 
murph1a 
grays1z 
howar1c 
howar1s 
tiure1jd 
thare1b 
lanni1t 
clega1s 
bomba1t 
balta1g 
brand1m 
took1p 
crush1w 
corey1c 
edrie1m 
grube1h 
sobch1w 

MT01 
---- 
derit1cj 
tai1db 
hego1d 
stark1a 
bomba1t 
took1p 
scott1m 
crush1w 
grube1h 
rugen1t 
solo1h 
corey1c 
flaum1e 
talto1v 
mccoy1e 
carpe1m 
sobch1w 

CS01 
---- 
howar1m 
howar1c 
fine1l 
tiure1jd 
thare1b 
tai1db 
lanni1t 
stark1a 
bomba1t 
mccoy1l 
monto1i 
solo1h 
biggs1gj 
corey1c 
edrie1m 
carpe1m 

CS05 
---- 
grays1z 
adama1w 
adama1l 
rosli1l 
balta1g 
tigh1e 
tigh1s 
cottl1s 
zarek1t 
murph1a 
sobch1w 
dane1a 

EN08 
---- 
grays1z 
adama1w 
adama1l 
rosli1l 
balta1g 
tigh1e 
tigh1s 
cottl1s 
zarek1t 
murph1a 
grube1h 
biggs1gj 

OT02 
---- 
adama1w 
adama1l 
tigh1s 
scott1m 
zarek1t 
murph1a 

我需要在文本文件中讀取創建使用兩種Student對象檔案和'學生'班。 類是:

class Student (object): 
    def __init__(self, first_name, middle_name, last_name, student_id, enrolled_courses): 
     """Initialization method""" 
     self.first_name = first_name 
     self.middle_name = middle_name 
     self.last_name = last_name 
     self.student_id = student_id 
     self.enrolled_courses = enrolled_courses 

,並在主要方法,我有:

if __name__ == '__main__': 
    list_of_students = [] 
    with open('Students.txt') as f: 
     for line in f: 
       data = line.split() 
       if len(data) == 3: 
        first_name, last_name, student_id = data 
        list_of_students.append(Student(last_name, '', first_name, student_id)) 
       elif len(data) == 4: 
        list_of_students.append(Student(*data)) 
       else: 
        continue 

當我沒有運行該程序的enrolled_courses變量,只有在「Students.txt」讀,它運行完美,使用first_name,middle_name,last_namestudent_id創建Student對象。不過,我仍然需要使用將enrolled_courses變量添加到對象並從「EnrolledCourses.txt」中獲取它。我如何讀取這兩個文件並將變量分配給我正在創建的對象?

回答

1

首先閱讀您的學生/課程,並創建一個字典:關鍵=學生,值=課程

格式是奇怪,但下面的代碼已經過測試和作品清單(也許不夠健壯,因爲它應該,雖然)。逐行閱讀,課程優先,學生名單。添加到字典(如果創建鍵不存在空單,漂亮defaultdict對象不說):

from collections import defaultdict 

student_course = defaultdict(list) 
with open("CourseEnrollment.txt") as enr: 
    while True: 
    try: 
     course_name = next(enr).strip() 
     next(enr) # skip dashes 
     while True: 
      student = next(enr).strip() 
      if student=="": 
       break 
      student_course[student].append(course_name) 
    except StopIteration: 
     break 

,並在你的代碼,調用Student構造是這樣的:

list_of_students.append(Student(last_name, '', first_name, student_id, student_course[student_id]))