SHARE
TWEET

Untitled

a guest Nov 17th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import re
  2. import time
  3.  
  4. start_time = time.clock()
  5. json_output = ""
  6.  
  7.  
  8. def load_xml(path):
  9.     with open("schedule.xml", encoding="utf-8") as r:
  10.         my_xml = r.read()
  11.     return my_xml
  12.  
  13.  
  14. def write_json_to_file(json_to_write):
  15.     f = open("result.json", "a")
  16.     f.write(json_to_write + "\n")
  17.     f.close()
  18.  
  19.  
  20. xml_list = load_xml("schedule.xml")
  21.  
  22. tags = re.findall('</(.*?)>', xml_list)[::-1]
  23.  
  24. group = re.search('group="(.*?)"', xml_list).group(1)
  25. day = re.search('day="(.*?)"', xml_list).group(1)
  26. sub_tag = re.search('<(.*?)/>', xml_list).group(1).split(' ')[0]
  27.  
  28. lessons_times = re.findall('time="(.*?)"', xml_list)
  29. lessons_rooms = re.findall('room="(.*?)"', xml_list)
  30. lessons_places = re.findall('place="(.*?)"', xml_list)
  31. lessons_names = re.findall('lesson="(.*?)"', xml_list)
  32. lessons_parity = re.findall('parity="(.*?)"', xml_list)
  33. lessons_teachers = re.findall('teacher="(.*?)"', xml_list)
  34.  
  35.  
  36. tag_length = 0
  37. extra_length = 0
  38. for i in range(0, len(tags)+2):
  39.     if i == 0:
  40.         tag_line = '{{ \n  "{0}": {{\n'.format(tags[0])
  41.         json_output += tag_line
  42.         tag_length = len(tag_line.replace(" ", ""))
  43.         json_output += tag_length*" " + '"group":"{0}",\n'.format(group)
  44.         json_output += tag_length*" " + '"day":"{0}",\n'.format(day)
  45.     if i == 1:
  46.         tag_line = '"{0}": {{\n'.format(tags[1])
  47.         json_output += tag_length*" " + '"{0}": {{\n'.format(tags[1])
  48.         tag_length += len(tag_line.replace(" ", ""))
  49.     if i == 2:
  50.         tag_line = '"{0}": [\n'.format(sub_tag)
  51.         json_output += tag_length*" " + tag_line
  52.         extra_length = tag_length + len(tag_line.replace(" ", ""))
  53.     if i == 3:
  54.         for j in range(0, len(lessons_times)):
  55.             json_output += tag_length * " " + "{\n"
  56.             json_output += str.format(extra_length * " " + '"time": "{0}",\n', lessons_times[j])
  57.             json_output += str.format(extra_length * " " + '"room": "{0}",\n', lessons_rooms[j])
  58.             json_output += str.format(extra_length * " " + '"place": "{0}",\n', lessons_places[j])
  59.             json_output += str.format(extra_length * " " + '"lesson": "{0}",\n', lessons_names[j])
  60.             json_output += str.format(extra_length * " " + '"чётность": "{0}",\n', lessons_parity[j])
  61.             json_output += str.format(extra_length * " " + '"teacher": "{0}"\n', lessons_teachers[j])
  62.             json_output += tag_length * " " + "},\n"
  63.             if j == len(lessons_times)-1:
  64.                 json_output = json_output[:-2]
  65.                 json_output += "\n" + tag_length * " " + "]\n"
  66.  
  67. tag_length = tag_length - len('"{0}": {{\n'.format(tags[1]))
  68. json_output += tag_length*" " + " }\n"
  69. tag_length = tag_length - len('{{\n  "{0}": {{\n'.format(tags[0]))
  70. json_output += "  }\n"
  71. json_output += "}"
  72. print(json_output)
  73. print(time.perf_counter() - start_time)
  74.  
  75. write_json_to_file(json_output)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top