import json import csv import codecs def decode_unicode_escapes(data): if isinstance(data, str): return codecs.decode(data, 'unicode_escape') elif isinstance(data, dict): return {key: decode_unicode_escapes(value) for key, value in data.items()} elif isinstance(data, list): return [decode_unicode_escapes(element) for element in data] else: return data # Load data from JSON file with UTF-8 encoding with open('data.json', 'r', encoding='utf-8') as f: raw_data = f.read() # Decode Unicode escape sequences decoded_data = decode_unicode_escapes(raw_data) # Parse the JSON data data = json.loads(decoded_data) teachers = data['teachers'] classes = data['classes'] subjects = data['subjects'] classrooms = data['classrooms'] teacherid = 1 teacherEduMap = {} teacherClassroomMap = {} teacher_dicts = [] for eduID, teacher in teachers.items(): firstname = teacher["firstname"] lastname = teacher["lastname"] isFemale = 0 if teacher["gender"] == "M" else 1 classroom = teacher["classroomid"] or 0 if classroom: teacherClassroomMap[classroom] = teacherid short = teacher["short"] startedTeaching = teacher["datefrom"] teacher_dicts.append([teacherid, eduID, firstname, lastname, isFemale, classroom, short, startedTeaching]) teacherEduMap[eduID] = teacherid teacherid += 1 subjectID = 1 subjectEduMap = {} subject_dicts = [] for eduID, subject in subjects.items(): name = subject["name"] short = subject["short"] subject_dicts.append([subjectID, eduID, name, short]) subjectEduMap[eduID] = subjectID subjectID += 1 classroomID = 1 classroomEduMap = {} classroom_dicts = [] for eduID, classroom in classrooms.items(): name = classroom["name"] short = classroom["short"] teacher = teacherClassroomMap.get(eduID, 0) classroom_dicts.append([classroomID, eduID, name, short, teacher]) classroomEduMap[eduID] = classroomID classroomID += 1 classID = 1 classEduMap = {} class_dicts = [] for eduID, classobj in classes.items(): name = classobj["name"] short = classobj["short"] grade = classobj["grade"] classTeacher = teacherEduMap.get(classobj["teacherid"], 0) backupTeacher = teacherEduMap.get(classobj["teacher2id"], 0) classroom = classroomEduMap.get(classobj["classroomid"], 0) class_dicts.append([classID, eduID, grade, name, short, classTeacher, backupTeacher, classroom]) classEduMap[eduID] = classID classID += 1 for teacher in teacher_dicts: if teacher[5] not in classroomEduMap: classroomEduMap[teacher[5]] = 0 teacher_dicts[teacher[0] - 1][5] = classroomEduMap[teacher[5]] # Write dictionaries to CSV files with open('teachers.csv', 'w', encoding="utf-8", newline='') as csvfile: writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) writer.writerows(teacher_dicts) with open('subjects.csv', 'w', encoding="utf-8", newline='') as csvfile: writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) writer.writerows(subject_dicts) with open('classrooms.csv', 'w', encoding="utf-8", newline='') as csvfile: writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) writer.writerows(classroom_dicts) with open('classes.csv', 'w', encoding="utf-8", newline='') as csvfile: writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) writer.writerows(class_dicts)