Neculai Fântânaru

Everything Depends On The Leader

Python: Replaces the string ( ) with a space, and then removes all duplicate spaces from html tags

On Noiembrie 23, 2021
, in
Python Scripts Examples by Neculai Fantanaru

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

Install Python.

Python code will replace the string (&nbsp;) with a space and remove any duplicate spaces in the html tags that are between the two boundaries < ! -- ARTICOL START --> şi < ! -- ARTICOL FINAL -->.

It will also delete any empty space at the beginning and end of each line that is contained in the html tags. I only took the tags into account <p>..</p> si <p><em>..</em></p>

<!-- ARTICOL START -->

<p class="obisnuit"><em>    Honor &nbsp;your  moral and spiritual      obligations &nbsp;  .</em></p>
<p class="nint">&nbsp;  Bishop  knew how to say the    most meaningful &nbsp;    of things  speech.&nbsp; </p>

<!-- ARTICOL FINAL -->

Will become:

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

THE CODE: Copy and run the code below in any interpreter program (I use pyScripter) . Don't forget to change the path on the "directory_name =" line.

import re
import os


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 replace_white_spaces(tag_name, file_path):
    """
    Aceasta functie modifica textul dintre un tag dat ca argument.
    """

    text = read_text_from_file(file_path)

    text = str(text)

    articol_pattern = re.compile('<!-- ARTICOL START -->[\s\S]*?<!-- ARTICOL FINAL -->[\s\S]*?')
    text_articol = re.findall(articol_pattern, text)
    if len(text_articol) != 0:
        text_articol = str(text_articol[0])
        pattern = re.compile('<{} class=\".*?\">(.*?)</{}>'.format(tag_name, tag_name))  
       
        tag_texts = re.findall(pattern, text_articol)
       
        new_text_articol = text_articol
        for tag_text in tag_texts:
          
           
           
            new_text = tag_text.strip()
            m = re.findall('<em>(.*?)</em>', new_text)
            if len(m) >= 1:
                text_em = str(m[0])
                text_em_new = text_em
                text_em_new = text_em_new.replace(r'&nbsp;', r' ')
                text_em_new = text_em_new.strip()
                new_text = new_text.replace(text_em, text_em_new)
            
            new_text = new_text.replace(r'&nbsp;', r' ')
            new_text = " ".join(new_text.split())
            
            new_text_articol = new_text_articol.replace(tag_text, new_text)
        
        text = text.replace(text_articol, new_text_articol)
        write_to_file(text, file_path)
        print("Fisierul modificat cu succes este: ", file_path)
    else:
        print("Fisierul nu are structura corecta: ", file_path)




def gaseste_nbsp(file_path):
    text_Reg = f.read()


def replace_white_spaces_only_html_php(tag_name, directory_name):
    for root, dirs, files in os.walk(directory_name):
        for f in files:
            if f.endswith('html'):
                file_path = os.path.join(root, f)
                replace_white_spaces(tag_name, file_path)
            else:
                continue

if __name__ == '__main__':  
    
    
    directory_name = 'c:\\Folder1'
   
    tag_name = 'p'
    
    replace_white_spaces_only_html_php(tag_name, directory_name)

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.