import os BASE_DIR = os.path.join("d:\\", "_gogs", "TZI", "Лекции", "ПМ3.1") readme_filename = os.path.join(BASE_DIR, "README.md") # словарь вопросов и ответов sample_quests = {} # открываем файл f = open(readme_filename, "r", encoding="utf-8") # читаем файл readme_str = f.read() f.close() # преобразуем строку в массив построчно readme_list = readme_str.split("\n") # перебираем массив и ищем слово "Вопросы" new_quest_str = "" for readme_line in readme_list: if "Вопросы" in readme_line: # отделяем левую ненужную часть строки t, filename_path = readme_line.split("Вопросы](") # отделяем правую ненужную часть строки filename_path, t = filename_path.split(")") filename_path = os.path.join(BASE_DIR, filename_path) # new_quest_str += f"{filename_path}\n" # открываем файл с вопросами f_quest = open(filename_path, "r", encoding="utf-8") quest_str = f_quest.read().strip() quest_list = quest_str.split("\n") current_type = "quest" current_quest = "" current_answ = "" for line in quest_list: if line == "": sample_quests[current_quest] = current_answ new_quest_str += f"{current_quest}\n{current_answ}\n\n" current_type = "quest" current_quest = "" current_answ = "" else: if current_type == "answ": current_answ = line.strip().replace("\ufeff", "") if current_type == "quest": current_type = "answ" current_quest = line.strip().replace("\ufeff", "") f_quest.close() quest_filename = os.path.join(BASE_DIR, "quest-tzi-pm31.md") f_quest = open(quest_filename, "w+", encoding="utf-8") f_quest.write(new_quest_str) f_quest.close()