#Some code i wrote as an exercise one day. class Students: def __init__(self): self.students = {} self.active_students = {} def add_student(self, header,row): user_id = row[header.index('user_id')] user_name = row[header.index('user_name')] state = row[header.index('state')] assert(user_id and user_name and state != None) self.students[user_id] = (user_name, state) def get_active_students(self): for key in self.students: if self.students[key][1] == 'active': self.active_students[key] = self.students[key] return self.active_students class Courses: def __init__(self): self.courses = {} self.active_courses = {} def add_course(self, header,row): course_id = row[header.index('course_id')] course_name = row[header.index('course_name')] state = row[header.index('state')] assert(course_id and course_name and state != None) self.courses[course_id] = (course_name, state) def get_active_courses(self): for key in self.courses: if self.courses[key][1] == 'active': self.active_courses[key] = self.courses[key] return self.active_courses class Enrollments: def __init__(self): self.enrollments = {} self.active_enroll = {} def add_enrollment(self, header,row): user_id = row[header.index('user_id')] course_id = row[header.index('course_id')] state = row[header.index('state')] assert(user_id or course_id or state != None) self.enrollments[(user_id, course_id)] = (state) def get_active_enrollments(self): for key in self.enrollments: if self.enrollments[key] == 'active': self.active_enroll[key] = self.enrollments[key] return self.active_enroll import glob import csv def __main__(): #first get the list of files that we need to parse students = Students() courses = Courses() enrollments = Enrollments() csv_files = glob.glob("../csvs/*.csv") for csv_file in sorted(csv_files): with open(csv_file, 'rb') as csvfile: csv_reader = csv.reader(csvfile) ''' You'll need to determine the type of data in the csv based on the headers in the first row. ''' header = csv_reader.next() if 'user_id' in header and 'course_id' in header: #Build Enrollment Objects here for row in csv_reader: enrollments.add_enrollment(header,row) elif 'user_id' in header: #Build Student Objects here for row in csv_reader: students.add_student(header,row) elif 'course_id' in header: #Build Course Objects here for row in csv_reader: courses.add_course(header,row) #At the end, you need to spit out a list of active courses, and for each course a list #of active students with active enrollments in that course active_courses = courses.get_active_courses() active_students = students.get_active_students() active_enrollments = enrollments.get_active_enrollments() for course_id in active_courses: print "\nCourse: " ,active_courses[course_id][0] for user_id in active_students: try: if active_enrollments[(user_id, course_id)] == 'active': print active_students[user_id][0] except KeyError: pass __main__()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question