Puteţi vizualiza întregul cod aici: https://pastebin.com/vA56f8pN
Instalaţi Python.
Să spunem că avem mai multe fişiere .txt. Şi avem doar un singur fişier .html
În corpul fişierului .html există această secţiune: < ! -- ARTICOL START --> şi < ! -- ARTICOL FINAL -->. Aici se va copia textul din fisierul .txt.
De asemenea, va trebui să creaţi un folder numit: fisiere_html (aici se vor salva toate fişierele noi). Toate celelalte fişiere . txt si fişierul .html se află libere în afara folderului: fisiere_html
Puteţi avea oricâte fişiere .txt doriţi. Un asemenea fişier .txt va conţine doar text, de genul:
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.
Veţi avea nevoie doar de un singur fişier .html care să conţină următoarea secţiune:
<!-- 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 -->
OUTPUT: după ce rulăm codul, vom avea:
<!-- 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: Copiaţi şi rulaţi codul de mai jos în orice program interpreter (eu folosesc pyScripter) . Nu uitaţi să schimbaţi calea din linia "creare_fisiere_html".
import os import re def read_text_from_file(file_path): """ Aceasta functie returneaza continutul unui fisier. file_path: calea catre fisierul din care vrei sa citesti """ with open(file_path, encoding='utf8') as f: text = f.read() return text def write_to_file(text, file_path): """ Aceasta functie scrie un text intr-un fisier. text: textul pe care vrei sa il scrii file_path: calea catre fisierul in care vrei sa scrii """ with open(file_path, 'wb') as f: f.write(text.encode('utf8', 'ignore')) def copiaza_continut_txt_html(cale_fisier_txt, cale_fisier_html): # astea sunt argumentele functiei, adica cand apelez functia # citesti textul din fisier 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 pattern-ul pentru expresia regex; (.*?) inseamna ca preia tot ce este intre tag-uri # modifici expresia regulata in functie de ce tag dai ca argument pentru functie articol_pattern = re.compile('<!-- 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' 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, file_path) print("Scriere efectuata cu succes.") else: print("Fisier html fara ARTICOL START/FINAL.") def creare_fisiere_html(cale_folder_txt, cale_fisier_html): """ Functia itereaza printr-un folder care contine fisiere txt si creeaza fisiere html corespunzatoare """ for f in os.listdir(cale_folder_txt): if f.endswith('txt'): cale_fisier_txt = cale_folder_txt + "\\" + f copiaza_continut_txt_html(cale_fisier_txt, cale_fisier_html) else: continue def main(): creare_fisiere_html("c:\\Folder1", "c:\\Folder1\\oana.html") if __name__ == '__main__': main()
That's all folks.
If you like my code, then make me a favor: translate your website into Romanian, "ro".
Also, you can see other Python Codes: VERSION 2 of this code. Or Version 3 OR Version 4 OR Version 5
Puteţi vizualiza şi versiunea de cod în PowerShell or VERSION 2 or VERSION 3