1+ from typing import List , Dict
12from bs4 import BeautifulSoup
3+ from collections import defaultdict
24
35
4- def parse_exam_schedule (html : str ) -> dict :
6+ def parse_exam_schedule (html : str ) -> List [ Dict ] :
57 soup = BeautifulSoup (html , "html.parser" )
68 schedule_table = soup .find ("table" )
79
810 if not schedule_table :
9- return {"error " : "No timetable found" }
11+ return [ {"error" : "No timetable found" }]
1012
1113 rows = schedule_table .find_all ("tr" )
12- exam_schedule = {}
14+ exam_schedule = defaultdict ( list )
1315 current_exam_type = None
1416
1517 for row in rows :
@@ -19,13 +21,13 @@ def parse_exam_schedule(html: str) -> dict:
1921 # Skip rows that don't contain enough values or are headers
2022 if len (values ) == 1 :
2123 current_exam_type = values [0 ]
22- exam_schedule [current_exam_type ] = {}
2324 continue
2425 elif len (values ) < 13 or "S.No." in values :
2526 continue
2627
2728 # Construct the data dictionary
2829 exam_entry = {
30+ "serial_number" : values [0 ],
2931 "course_code" : values [1 ],
3032 "course_title" : values [2 ],
3133 "type" : values [3 ],
@@ -40,7 +42,17 @@ def parse_exam_schedule(html: str) -> dict:
4042 "seat_number" : values [12 ],
4143 }
4244
43- # Add the entry to the dictionary under the current exam type
44- exam_schedule [current_exam_type ][values [0 ]] = exam_entry
45+ # Add the entry to the list under the current exam type
46+ if current_exam_type :
47+ exam_schedule [current_exam_type ].append (exam_entry )
4548
46- return exam_schedule
49+ # Convert defaultdict to a list of dictionaries
50+ result = [
51+ {
52+ "exam_type" : exam_type ,
53+ "subjects" : subjects
54+ }
55+ for exam_type , subjects in exam_schedule .items ()
56+ ]
57+
58+ return result
0 commit comments