commit 06fa7f096d598f64bf98e6f0e58af125c837d4bd Author: Bruno Rybársky Date: Tue Jun 25 22:35:49 2024 +0200 Init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9ddba91 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.venv +*.json +*.csv \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/adlimport.iml b/.idea/adlimport.iml new file mode 100644 index 0000000..2c80e12 --- /dev/null +++ b/.idea/adlimport.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c2640aa --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6281dd5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..b7e3b91 --- /dev/null +++ b/main.py @@ -0,0 +1,102 @@ +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) \ No newline at end of file