Полный код можно просмотреть здесь: https://pastebin.com/vA56f8pN.
Установите Python.
Допустим, у нас есть несколько файлов .txt. И у нас есть только один файл .html.
В теле файла .html есть этот раздел: < ! -- ARTICOL START --> и < ! -- АРТИКОЛЬ ФИНАЛ -->. Сюда будет скопирован текст из файла .txt.
Вам также потребуется создать папку с именем: fisiere_html (здесь будут сохраняться все новые файлы). Все остальные файлы. txt и файл .html находятся вне папки: fisiere_html
Вы можете иметь столько файлов .txt, сколько захотите. Такой файл .txt будет содержать только текст, например:
My name probably does not say anything to you, Walter Elias Disney,
because I just came back from New York and got only 40 dollars in my pocket.
Increasingly, I need spiritual points to help me not get swallowed by the
most painful miscarriage, the saddest abandon in a labyrinth of vanity
where the only marks are self-forgetting and the others, followed in the
end of supreme sacrifice.
Вам понадобится только один файл .html, содержащий следующий раздел:
<!-- ARTICOL START --> <p class="obisnuit"><em>Honor your moral and spiritual obligations.</em></p> <p class="nint">Bishop knew how to say the most meaningful of things speech.</p> <!-- ARTICOL FINAL -->
ВЫВОД: после запуска кода мы получим:
<!-- ARTICOL START -->
My name probably does not say anything to you, Walter Elias Disney,
because I just came back from New York and got only 40 dollars in my pocket.
Increasingly, I need spiritual points to help me not get swallowed by the
most painful miscarriage, the saddest abandon in a labyrinth of vanity
where the only marks are self-forgetting and the others, followed in the
end of supreme sacrifice.
<!-- ARTICOL FINAL -->CODUL: скопируйте и запустите приведенный ниже код в любой программе-интерпретаторе (я использую pyScripter) . Не забудьте изменить путь в строке "creare_fisiere_html".
import os импорт re def read_text_from_file(file_path): """ Эта функция возвращается в постоянное время. путь_файла: calea catre fisierul din care vrei sa citesti """ с открытым(file_path,coding='utf8') как е: text = f.read() возврат текста def write_to_file(text, file_path): """ Эта функция позволяет записывать вводимый текст. text: textul pe care vrei sa il scrii путь_файла: calea catre fisierul in care vrei sa scrii """ с открытым(file_path, 'wb') как f: f.write(text.encode('utf8', 'игнорировать')) def copiaza_continut_txt_html(cale_fisier_txt, cale_fisier_html): # astea sunt аргументеле functiei, adica cand apelez functia # цитата из текста text_txt = read_text_from_file(cale_fisier_txt) text_html = read_text_from_file(cale_fisier_html) # Transformam textul din fisier intr-un string text_txt = str(text_txt) text_html = str(text_html) # aici e шаблон-ul pentru expresia regex; (.*?) inseamna ca preia tot ce este intre tag-uri # изменение выражения в функции этого тега, дай-ка аргумент для каждой функции articol_pattern = re.compile(& #39;<!-- ARTICOL START -->([\s\S]*?)<!-- ARTICOL FINAL -->[\s\S]*?') text_articol = re.findall(articol_pattern, text_html) if len(text_articol) != 0: text_articol = str(text_articol[0]) text_txt = '\n\n' + text_txt + '\n\n span>' text_html = text_html.replace(text_articol, text_txt) file_path = os.path.dirname(cale_fisier_txt) + "\\" + "fisiere_html" + "\\" + os.path.splitext(os.path.basename(cale_fisier_txt) )[0] + '.html' write_to_file(text_html, путь_файла) print("Scriere efectuata cu succes.") иначе: print("Fisier html для ARTICOL START/FINAL." пролет>) def creare_fisiere_html(cale_folder_txt, cale_fisier_html): """ Функция «Уход за папкой printr-un» позволяет сохранять файлы txt и создавать файлы html corespunzatoare """ для f в ОС.listdir(cale_folder_txt): if f.endswith('txt'): cale_fisier_txt =cale_folder_txt + "< /span>\\" span> + f copyiaza_continut_txt_html(cale_fisier_txt, cale_fisier_html) иначе: продолжить def main(): creare_fisiere_html("c:\\< /span>Папка1", "c:\\Folder1\\oana.html") if __name__ == '__main__': основной()
Вот и все, ребята.
If you like my code, then make me a favor: translate your website into Romanian, "ro".
Также ознакомьтесь с этой ВЕРСИЕЙ 2 или ВЕРСИЯ 3 или ВЕРСИЯ 4 или ВЕРСИЯ 5 или ВЕРСИЯ 6 или ВЕРСИЯ 7