ro  fr  en  es  pt  ar  zh  hi  de  ru
ART 2.0 ART 3.0 ART 4.0 ART 5.0 ART 6.0 Pinterest

पायथन: HTML टैग में डबल रिक्त स्थान हटाएं

On Noiembrie 23, 2021, in Leadership and Attitude, by Neculai Fantanaru

आप यहां पूरा कोड देख सकते हैं:Https: // passatin.com / ओह 2 वी वाई 70

इंस्टॉलपायथन

पायथन कोड सभी HTML टैग ढूंढता है जिसमें शब्दों के बीच डबल रिक्त स्थान होते हैं, और शब्दों के बीच एक खाली स्थान छोड़ देंगे।

यह एचटीएमएल टैग में निहित प्रत्येक पंक्ति की शुरुआत और अंत में किसी भी खाली स्थान को भी हटा देगा। मैंने केवल टैग को ध्यान में रखा

.. चार

..

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

हो जाएगा:

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

कोड: किसी भी दुभाषिया कार्यक्रम में नीचे दिए गए कोड को कॉपी और चलाएं(मैं उपयोग करता हूंPyscrcs"Directory_name =" लाइन पर पथ को बदलने के लिए मत भूलना।

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.
    """
    # citesti textul din fisier
    text = read_text_from_file(file_path)
    # transformam textul din fisier intr-un string
    text = str(text)
    # 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
    pattern = re.compile('<{} class=\".*?\">(.*?)'.format(tag_name, tag_name))  
    # aici se preiau toate textele dintre tag-uri
    tag_texts = re.findall(pattern, text)
    for tag_text in tag_texts:
        # strip taie toate spatiile de la inceputul si finalul text-ului
        new_text = tag_text.strip()
        m = re.findall('(.*?)', new_text)
        if len(m) >= 1:
            text_em = str(m[0])
            text_em_new = text_em.strip()
            new_text = new_text.replace(text_em, text_em_new)
        # facem split la text dupa spatiu si apoi unim cuvintele gasite printr-un singur spatiu
        new_text = " ".join(new_text.split())
        # textul nou va fi textul initial, dar care are textul dintre tag-uri inlocuit cu textul prelucrat
        text = text.replace(tag_text, new_text)
    # la final suprascriem continutul initial al fisierului cu noul continut
    write_to_file(text, file_path)


def replace_white_spaces_only_html_php(tag_name, directory_name):
    for file in os.listdir(directory_name):
        filename = str(file)
        print(filename)
        # verificam daca fisierul se termina cu extensia html sau php
        if filename.endswith(".html") or filename.endswith(".php"):
            file_path = os.path.join(directory_name, filename)
            # pentru fiecare fisier gasit, stergem spatiile in plus
            replace_white_spaces(tag_name, file_path)
        else:
            continue


if __name__ == '__main__':
    # setezi numele folderului
    # nu uita de slash-urile duble
    directory_name = "c:\\Folder2\\5"
    # setezi numele tag-ului
    tag_name = 'p'
    # apelezi functia care itereaza prin director
    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".

इसके अलावा, एक हैसंस्करण 2इस कोड यासंस्करण 3यासंस्करण 4यासंस्करण 5यासंस्करण 6

 


Latest articles accessed by readers:

  1. An Eye To See And A Mind To Understand
  2. Turn Towards Me With An Eye Full Of Your Own Gaze
  3. The Snapshot Of Magic In God's Universe
  4. Rhythm Of My Heart

Donează prin Paypal

Alternate Text

DONAŢIE RECURENTĂ

Donează lunar pentru susţinerea proiectului NeculaiFantanaru.com

DONAŢIE SINGULARĂ

Donează suma dorită pentru susţinerea proiectului NeculaiFantanaru.com

Donează prin Transfer Bancar

Cont Lei: RO34INGB0000999900448439

Deschis la ING Bank

Alatura-te Comunitatii Neculai Fantanaru

decoration
About | Site Map | Partners | Feedback | Terms & Conditions | Privacy | RSS Feeds
© Neculai Fântânaru - All rights reserved