1
0

quests2quests.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import os
  2. BASE_DIR = os.path.join("d:\\", "_gogs", "TZI", "Лекции", "ПМ3.1")
  3. readme_filename = os.path.join(BASE_DIR, "README.md")
  4. # словарь вопросов и ответов
  5. sample_quests = {}
  6. # открываем файл
  7. f = open(readme_filename, "r", encoding="utf-8")
  8. # читаем файл
  9. readme_str = f.read()
  10. f.close()
  11. # преобразуем строку в массив построчно
  12. readme_list = readme_str.split("\n")
  13. # перебираем массив и ищем слово "Вопросы"
  14. new_quest_str = ""
  15. for readme_line in readme_list:
  16. if "Вопросы" in readme_line:
  17. # отделяем левую ненужную часть строки
  18. t, filename_path = readme_line.split("Вопросы](")
  19. # отделяем правую ненужную часть строки
  20. filename_path, t = filename_path.split(")")
  21. filename_path = os.path.join(BASE_DIR, filename_path)
  22. # new_quest_str += f"{filename_path}\n"
  23. # открываем файл с вопросами
  24. f_quest = open(filename_path, "r", encoding="utf-8")
  25. quest_str = f_quest.read().strip()
  26. quest_list = quest_str.split("\n")
  27. current_type = "quest"
  28. current_quest = ""
  29. current_answ = ""
  30. for line in quest_list:
  31. if line == "":
  32. sample_quests[current_quest] = current_answ
  33. new_quest_str += f"{current_quest}\n{current_answ}\n\n"
  34. current_type = "quest"
  35. current_quest = ""
  36. current_answ = ""
  37. else:
  38. if current_type == "answ":
  39. current_answ = line.strip().replace("\ufeff", "")
  40. if current_type == "quest":
  41. current_type = "answ"
  42. current_quest = line.strip().replace("\ufeff", "")
  43. f_quest.close()
  44. quest_filename = os.path.join(BASE_DIR, "quest-tzi-pm31.md")
  45. f_quest = open(quest_filename, "w+", encoding="utf-8")
  46. f_quest.write(new_quest_str)
  47. f_quest.close()