Skip to content

Commit

Permalink
last commit was not the latest.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhs04001 committed Sep 8, 2021
1 parent 497211a commit 8bd96e3
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions cleanup-roster.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# import system module
import sys, re, argparse
import csv

class Students:
class Student:

def __init__(self):
self.id = ""
Expand Down Expand Up @@ -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('<tr>') or line.startswith('</table>'):
if student is not None:
student_list.append(student)
field_num = -1
student = None
if line.startswith('<tr>') or line.startswith('</table>'):
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('<td'):
previous = ''
else:
line = previous + line

if not line.endswith('</td>'):
previous = line
continue

# now we have a line that starts with "<td" and ends with "</td>"
m = re.search(r'^<td[^>]*>(.*)</td>', 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("<td"):
# ignore lines that do not indicate a new field
assert(previous == '')
continue

# print(line)

# now we check if a line that starts with "<td" and has a "</td>"
m = re.match(r'<td[^>]*>(.*)</td>', 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:
Expand Down

0 comments on commit 8bd96e3

Please sign in to comment.