1

I need to use a 'Student' class with 5 variables and create objects using more than one file.

The text files: (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     

Text file 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

I need to read in the text files to create Student objects using both files and the 'Student' class. The class is:

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

and in the main method I have:

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

When I run the program without an enrolled_courses variable and only read in 'Students.txt', it runs perfect and creates the Student objects with first_name, middle_name, last_name and student_id. However, I still need to use add the enrolled_courses variable to the objects and obtain it from 'EnrolledCourses.txt'. How can I read in both files and assign the variables to the objects I'm trying to create?

1 Answers1

1

First read your student/course and create a dictionary: key=student, value=list of courses

The format is strange but the code below has been tested and works (maybe not as robust as it should, though). Read line by line, course first, and list of students. Add to dictionary (create empty list if key doesn't exist, nice defaultdict object does that):

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

and in your code, call Student constructor like this:

list_of_students.append(Student(last_name, '', first_name, student_id, student_course[student_id]))
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219