You can view the full code here: https://pastebin.com/ykzefx8c
Copiaţi acest cod în fişierul Python.txt
<html> <head> <link rel="context" href="www.peepee.com" spree="one"> <link spree="one" rel="context" href="www.peepee.com"> <link href="www.peepee.com" rel="context" spree="one"> </head> <body> </body> </html> ---- The Outputul will be: --- <html> <head> empty row empty row empty row </head> <body> </body> </html>
Codul Python de mai jos va face 3 operaţii Regex (Find + Replace), în ordinea în care am ales-o eu.
Option 1 - Replace in a single .txt file
import re import os with open('Python.txt','r') as f: text_citit_din_fisier = f.read() print("hello here: ", text_citit_din_fisier) # Regex1 FIND: <link (.*).*(href.*") REPLACE BY: <link \2 \1 text_citit_din_fisier=re.sub(r'<link (.*).*(href.*")', r'<link \2 \1', text_citit_din_fisier) print ("Primul Regex:", text_citit_din_fisier) # Regex2 FIND: spree.*> REPLACE BY: bebe text_citit_din_fisier=re.sub(r'spree.*>', r'bebe', text_citit_din_fisier) print ("Second Regex:", text_citit_din_fisier) # Regex3 FIND: (.*)bebe REPLACE BY: empty row/x20 text_citit_din_fisier=re.sub(r'(.*)bebe', r'empty row', text_citit_din_fisier) print ("Third Regex:", text_citit_din_fisier) with open("Python.txt", "w") as some_file_handle: some_file_handle.write(text_citit_din_fisier)
Option 2 - Replace in all .txt files from folder
import re import os # 0. Construim o functie care primeste ca argument un fisier si aplica niste expresii regulate def aplica_expresii_regulate(cale_fisier): with open(cale_fisier,'r') as f: text_citit_din_fisier = f.read() # Regex1 FIND: <link (.*).*(href.*") REPLACE BY: <link \2 \1 text_citit_din_fisier=re.sub(r'<link (.*).*(href.*")', r'<link \2 \1', text_citit_din_fisier) print ("Primul Regex:", text_citit_din_fisier) # Regex2 FIND: spree.*> REPLACE BY: bebe text_citit_din_fisier=re.sub(r'spree.*>', r'bebe', text_citit_din_fisier) print ("Second Regex:", text_citit_din_fisier) # Regex3 FIND: (^.*)bebe REPLACE BY: empty row/x20 text_citit_din_fisier=re.sub(r'.*bebe', r'empty row', text_citit_din_fisier) print ("Third Regex:", text_citit_din_fisier) print(text_citit_din_fisier) with open(cale_fisier, "w") as h: h.write(text_citit_din_fisier) # 1. Construim o functie care primeste ca argument un director, iar pentru fiecare fisier din director facem o anumita operatie def parcurge_director(cale_director): for nume_fisier in os.listdir(cale_director): if nume_fisier.endswith(".txt") or nume_fisier.endswith(".png"): #daca incepe cu txt cale_completa_fisier = os.path.join(cale_director, nume_fisier) aplica_expresii_regulate(cale_completa_fisier) else: continue directory = r'd:\Downloads' parcurge_director(directory)
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