Neculai Fântânaru

Everything Depends On The Leader

Batch Processor - How to do multiple finds and replace with Python

On May 05, 2021
, in
Python Scripts Examples by Neculai Fantanaru

You can view the full code here: https://pastebin.com/ykzefx8c

Copy this code to the Python.txt file

<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> 

The Python code below will do 3 Regex operations (Find + Replace), in the order I chose it.

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)

Short Alternative

>>> t1 = re.sub(r"^.*?>(.+?)(?= \|).*?$", r"\1.html")
>>> t2 = re.sub(r"\s", r"-", t1)

That's all folks.

If you like my code, then make me a favor: translate your website into Romanian, "ro".

Also, see this VERSION 2 or VERSION 3 or VERSION 4 or VERSION 5 or VERSION 6 or VERSION 7

Alatura-te Comunitatii Neculai Fantanaru
The 63 Greatest Qualities of a Leader
Cele 63 de calităţi ale liderului

Why read this book? Because it is critical to optimizing your performance. Because it reveals the main coordinates after that are build the character and skills of the leaders, highlighting what it is important for them to increase their influence.

Leadership - Magic of Mastery
Atingerea maestrului

The essential characteristic of this book in comparison with others on the market in the same domain is that it describes through examples the ideal competences of a leader. I never claimed that it's easy to become a good leader, but if people will...

The Master Touch
Leadership - Magia măiestriei

For some leaders, "leading" resembles more to a chess game, a game of cleverness and perspicacity; for others it means a game of chance, a game they think they can win every time risking and betting everything on a single card.

Leadership Puzzle
Leadership Puzzle

I wrote this book that conjoins in a simple way personal development with leadership, just like a puzzle, where you have to match all the given pieces in order to recompose the general image.

Performance in Leading
Leadership - Pe înţelesul tuturor

The aim of this book is to offer you information through concrete examples and to show you how to obtain the capacity to make others see things from the same angle as you.

Leadership for Dummies
Leadership - Pe înţelesul tuturor

Without considering it a concord, the book is representing the try of an ordinary man - the author - who through simple words, facts and usual examples instills to the ordinary man courage and optimism in his own quest to be his own master and who knows... maybe even a leader.