#!/usr/bin/python from sys import argv filename = "class1" if len(argv) > 1: filename = argv[1] inFile = open(filename) class StudentRec: def __init__(self): self.reset() def reset(self): self.level = "" self.program1 = "" self.program2 = "" self.college1 = "" self.college2 = "" self.major1 = "" self.major2 = "" self.dept1 = "" self.dept2 = "" self.rank = "" self.year = "" self.credits = "" self.concentration1= "" self.concentration2 = "" def findField (self,line) : if line == "": return if line.find(':') == -1: return (title, value)= line.split(':\t') if title == 'Level': self.level = value.strip(); elif title == "Program": if self.program1 == "": self.program1 = value.strip() else: self.program2 = value.strip() elif title == "Admit Term": self.junk = value elif title == "Catalog Term": self.junk = value elif title == "College": if self.college1 == "": self.college1 = value.strip() else: self.college2 = value.strip() elif title == "Campus": junk = value elif title == "Major and Department": if self.major1 == "": (self.major1,self.dept1) = value.split(', ') self.major1=self.major1.strip() self.dept1=self.dept1.strip() else: (self.major2,self.dept2) = value.split(', ') self.major2=self.major2.strip() self.dept2=self.dept2.strip() elif title == "Major Concentration": if self.concentration1 == "": self.concentration1 = value .strip() else: self.concentration2 = value.strip() elif title == "Class": (self.rank, self.year) = value.split('/',1) self.rank = self.rank.strip() self.year = self.year.strip() elif title == "Credits": self.credits = value.strip() def Print(self): line = self.level line += ','+self.rank line += ','+self.year line += ','+self.program1 line += ','+'"'+self.college1+'"' line += ','+'"'+self.dept1 +'"' line += ','+self.major1 line += ','+self.concentration1 line += ','+self.program2 line += ','+'"'+self.college2 +'"' line += ','+'"'+self.dept2 +'"' line += ','+self.major2 line += ','+self.concentration2 print line haveone = False student = StudentRec() for line in inFile: line = line.strip() if line =="Record": if not haveone: haveone = True else: student.Print() student.reset() else: student.findField(line) student.Print()