-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
54 lines (48 loc) · 1.52 KB
/
parser.py
File metadata and controls
54 lines (48 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import json
from Course import Course
codes = ["CE", "CZ", "MH"]
with open("ce.txt", "r", encoding='utf-8') as f:
lines = f.readlines()
print(len(lines))
courses = []
descriptionFlag = False
for i in range(len(lines)):
# hack here
if(lines[i][0] == '*'):
lines[i] = lines[i][1:]
code = lines[i][:2]
AUIndex = lines[i].find("Acad Unit: ")
preReqIndex = lines[i].find("Pre-requisite: ")
if(code in codes):
courses.append(Course())
separator = lines[i].find(" ")
courses[-1].code = lines[i][:separator]
courses[-1].name = lines[i][separator+1:-1] # -1 to strip the newline
descriptionFlag = False
elif(AUIndex != -1):
# hack here, CE1004 AUs is "2 / 3
hackIndex = lines[i].find("/")
if(hackIndex == -1):
hackIndex = len(lines[i])
courses[-1].AU = int(lines[i][AUIndex+len("Acad Unit: "):hackIndex])
elif(preReqIndex != -1):
courses[-1].preReq = lines[i][preReqIndex+len("Pre-requisite: "):]
descriptionFlag = True
elif(descriptionFlag):
courses[-1].description += lines[i]
coursesList = []
for course in courses:
coursesList.append(course.__dict__)
coursesJson = json.dumps(coursesList)
with open("ce.json", "w") as f:
f.write(coursesJson)
##with open("json.txt", "r") as f:
## s = json.load(f)
##print(s)
##print(s[0]["name"])
print(len(courses))
print(courses[0].code)
print(courses[0].name)
print(courses[0].AU)
print(courses[0].preReq)
print(courses[0].description)