Python3: problème avec re.sub. Le sujet est résolu

Tout ce qui concerne la programmation.
Répondre
Avatar de l’utilisateur
PengouinPdt
Contributeur
Contributeur
Messages : 1343
Inscription : 23 avr. 2016, 23:37
Localisation : 47/FR
Diaspora* : https://framasphere.org/u/hucste
Contact :
Status : Hors-ligne

J'ai un problème avec l'usage de re.sub...

Pour un fichier template html donné :

Code : Tout sélectionner

<!DOCTYPE html>
<html lang="{LANG}">
<head>
  <title>{TITLE}</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="bootstrap.min.css">
  <script src="bootstrap.min.js"></script>
  <style>
  	body { background-color: #ccc; }
    /* Set height of the grid so .sidenav can be 100% (adjust if needed) */
    .row.content {height: auto;}
    
    /* Set gray background color and 100% height */
    .sidenav {
        background-color: #ccc;
        height: 100%;
    }
    
    .brd-l {
    	border-left: 1px dotted #333;
        background-color: #eee;
	}
    
    article { 
        background-color: #eee; 
        border-bottom: 1px solid #333;
        
    }
    
    header {
        border-bottom: 1px dotted #333;
        padding: 10px;
    }
    
    /* Set black background color, white text and some padding */
    footer {
        background-color: #555;
        color: #ccc;
        padding: 15px;
    }
    
    footer a {
        color: #fff;
    }
    footer a:hover {
        color: #ccc;
    }
    
    /* On small screens, set height to 'auto' for sidenav and grid */
    @media screen and (max-width: 767px) {
        .sidenav {
            height: auto;
            padding: 15px;
        }
        .row.content {height: auto;} 
    }
  </style>
</head>
<body>

<div class="container-fluid">
  <div class="row content">
    <div class="col-sm-3 sidenav">
      
      <h1>{NAME} :: {DESCR}</h1>
        <h2>{ABOUT}</h2>
        <ul id="nav">
            <!-- {LINK} -->
        </ul>
      
    </div>

    <div class="col-sm-9 brd-l">
      
    	<section>
            <!-- {ARTICLE} -->
        </section>
      
    </div>
    
  </div>
</div>

<footer class="container-fluid">
  <p> - {NAME} :: <a href="{URL_WEB}">{DESCR}</a> - {COPYRIGHT} - {LICENCE}</p>
</footer>

</body>
</html>
J'utilisais le code suivant en py3 - (cf fichier : lignes 184 à 221) :

Code : Tout sélectionner

patterns = [
                '{ABOUT}',
                '{COPYRIGHT}',
                '{DESCR}',
                '{LANG}',
                '{LICENCE}',
                '{NAME}',
                '{TITLE}',
                '{URL_WEB}',
            ]
            replaces = [
                self.text['about']['historic'],
                self.text['about']['copyright'],
                self.DESCR, 
                self.config['lang'], 
                self.text['about']['licence'],
                self.NAME, 
                self.title['html'],
                self.url['web'],
            ]

            with open(filename, 'r') as rfile:
                lines = rfile.readlines()
                self.log.debug('=> Sed Lines :: Lines: %s' % lines)

            with open(filename, 'w') as wfile:
                for line in lines:
                    search = False

                    for key, pattern in enumerate(patterns):
                        if pattern in line:
                            wfile.write(re.sub(
                                r'' + pattern, replaces[key], line))
                            search = True

                    if not search:
                        wfile.write(line)
Mais au lieu de me les remplacer correctement, quand il y a plusieurs 'tags' dans mon template, sur une même ligne, il duplique ni + ni - la ligne et remplace juste le pattern correspondant !

Ainsi, il me restitue :

Code : Tout sélectionner

(...)
      <h1>{NAME} :: Yet Uploader Pixxie</h1>
      <h1>YUP :: {DESCR}</h1>
(...)
<footer class="container-fluid">
  <p> - {NAME} :: <a href="{URL_WEB}">{DESCR}</a> -  © 2015-2018 - {LICENCE}</p>
  <p> - {NAME} :: <a href="{URL_WEB}">Yet Uploader Pixxie</a> - {COPYRIGHT} - {LICENCE}</p>
  <p> - {NAME} :: <a href="{URL_WEB}">{DESCR}</a> - {COPYRIGHT} - License : <a href="LICENSE">OpenBSD</a></p>
  <p> - YUP :: <a href="{URL_WEB}">{DESCR}</a> - {COPYRIGHT} - {LICENCE}</p>
  <p> - {NAME} :: <a href="https://framagit.org/hucste/YUP.py/">{DESCR}</a> - {COPYRIGHT} - {LICENCE}</p>
</footer>
(...)
Une idée pour corriger mon code python ?
PengouinPdt { le seul, le vrai } ~ " Libre as a Pengouin "
- DIY - Debian Sid | Devuan Ceres
----
Ne réponds pas aux PM d'assistance
Avatar de l’utilisateur
PengouinPdt
Contributeur
Contributeur
Messages : 1343
Inscription : 23 avr. 2016, 23:37
Localisation : 47/FR
Diaspora* : https://framasphere.org/u/hucste
Contact :
Status : Hors-ligne

Beh, j'y suis arrivé en recodant plus simplement :

Code : Tout sélectionner

        with open(filename, 'w') as wfile:
                for line in lines:

                    for key, pattern in enumerate(patterns):
                        if pattern in line:
                            line = re.sub(r'' + pattern, replaces[key], line)

                    wfile.write(line)
PengouinPdt { le seul, le vrai } ~ " Libre as a Pengouin "
- DIY - Debian Sid | Devuan Ceres
----
Ne réponds pas aux PM d'assistance
Avatar de l’utilisateur
PengouinPdt
Contributeur
Contributeur
Messages : 1343
Inscription : 23 avr. 2016, 23:37
Localisation : 47/FR
Diaspora* : https://framasphere.org/u/hucste
Contact :
Status : Hors-ligne

Et, en utilisant re.combine :

Code : Tout sélectionner

                            reg = re.compile(r'' + pattern, re.MULTILINE)
                            line = reg.sub(replaces[key], line)
                            del(reg)
:icon_e_biggrin:
PengouinPdt { le seul, le vrai } ~ " Libre as a Pengouin "
- DIY - Debian Sid | Devuan Ceres
----
Ne réponds pas aux PM d'assistance
Répondre