Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import time
- start_time = time.clock()
- json_output = ""
- def load_xml(path):
- with open("schedule.xml", encoding="utf-8") as r:
- my_xml = r.read()
- return my_xml
- def write_json_to_file(json_to_write):
- f = open("result.json", "a")
- f.write(json_to_write + "\n")
- f.close()
- xml_list = load_xml("schedule.xml")
- tags = re.findall('</(.*?)>', xml_list)[::-1]
- group = re.search('group="(.*?)"', xml_list).group(1)
- day = re.search('day="(.*?)"', xml_list).group(1)
- sub_tag = re.search('<(.*?)/>', xml_list).group(1).split(' ')[0]
- lessons_times = re.findall('time="(.*?)"', xml_list)
- lessons_rooms = re.findall('room="(.*?)"', xml_list)
- lessons_places = re.findall('place="(.*?)"', xml_list)
- lessons_names = re.findall('lesson="(.*?)"', xml_list)
- lessons_parity = re.findall('parity="(.*?)"', xml_list)
- lessons_teachers = re.findall('teacher="(.*?)"', xml_list)
- tag_length = 0
- extra_length = 0
- for i in range(0, len(tags)+2):
- if i == 0:
- tag_line = '{{ \n "{0}": {{\n'.format(tags[0])
- json_output += tag_line
- tag_length = len(tag_line.replace(" ", ""))
- json_output += tag_length*" " + '"group":"{0}",\n'.format(group)
- json_output += tag_length*" " + '"day":"{0}",\n'.format(day)
- if i == 1:
- tag_line = '"{0}": {{\n'.format(tags[1])
- json_output += tag_length*" " + '"{0}": {{\n'.format(tags[1])
- tag_length += len(tag_line.replace(" ", ""))
- if i == 2:
- tag_line = '"{0}": [\n'.format(sub_tag)
- json_output += tag_length*" " + tag_line
- extra_length = tag_length + len(tag_line.replace(" ", ""))
- if i == 3:
- for j in range(0, len(lessons_times)):
- json_output += tag_length * " " + "{\n"
- json_output += str.format(extra_length * " " + '"time": "{0}",\n', lessons_times[j])
- json_output += str.format(extra_length * " " + '"room": "{0}",\n', lessons_rooms[j])
- json_output += str.format(extra_length * " " + '"place": "{0}",\n', lessons_places[j])
- json_output += str.format(extra_length * " " + '"lesson": "{0}",\n', lessons_names[j])
- json_output += str.format(extra_length * " " + '"чётность": "{0}",\n', lessons_parity[j])
- json_output += str.format(extra_length * " " + '"teacher": "{0}"\n', lessons_teachers[j])
- json_output += tag_length * " " + "},\n"
- if j == len(lessons_times)-1:
- json_output = json_output[:-2]
- json_output += "\n" + tag_length * " " + "]\n"
- tag_length = tag_length - len('"{0}": {{\n'.format(tags[1]))
- json_output += tag_length*" " + " }\n"
- tag_length = tag_length - len('{{\n "{0}": {{\n'.format(tags[0]))
- json_output += " }\n"
- json_output += "}"
- print(json_output)
- print(time.perf_counter() - start_time)
- write_json_to_file(json_output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement