diff --git a/cleanup-roster.py b/cleanup-roster.py index b6ffe0c..b5e6a68 100644 --- a/cleanup-roster.py +++ b/cleanup-roster.py @@ -1,8 +1,7 @@ -# import system module import sys, re, argparse import csv -class Students: +class Student: def __init__(self): self.id = "" @@ -57,39 +56,49 @@ def write_csv(file, student_list, fields): # print(args) student_list = [] -field_num = -1 student = None previous = '' -with open(args.infile, 'r') as file: - num_lines = 0 - for line in file: - num_lines += 1 +try: + with open(args.infile, 'r') as file: + num_lines = 0 + for line in file: + num_lines += 1 - if line.startswith('') or line.startswith(''): - if student is not None: - student_list.append(student) - field_num = -1 - student = None + if line.startswith('') or line.startswith(''): + if student is not None: + student_list.append(student) + student = None + previous = '' + continue - # remove spaces at the end of the line, including newline - line = line.rstrip() + # remove spaces at the end of the line, including newline + line = line.rstrip() - if line.startswith(''): - previous = line - continue - - # now we have a line that starts with "" - m = re.search(r'^]*>(.*)', line) - if m: - if student is None: - student = Students() - student.add_field(m.group(1)) + # append to previous line, if there is any + if previous: + line = previous + line + + if not line.startswith("" + m = re.match(r']*>(.*)', line) + if m: + if student is None: + student = Student() + student.add_field(m.group(1)) + previous = '' + else: + # if there is need more lines + previous = line +except FileNotFoundError as e: + print(e) + exit(1) if args.o != '': with open(args.o, 'w', newline='') as csvfile: