Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MORE DETAILS HERE:
- ENGLISH: https://neculaifantanaru.com/en/python-regex-batch-processor-multiple-find-and-replace.html
- ROMANIAN: https://neculaifantanaru.com/python-regex-batch-processor-multiple-find-and-replace.html
- # How to do multiple finds and replace with Python
- # The Python code below will do 3 Regex operations (Find + Replace), in the order I chose it.
- # Option 1 - Find and 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 - Find and 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement