Salut les web pythons,
Je développe un site avec le mod_python de apache et je rencontre ma première difficultée:
Je désire ouvrir un fichier html et écrire dedans avec des formatages du type:
%(variable)s
compris dans le fichier html.
par exemple:
sa ne marche pas d'accord, essayont autrement:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 a='''<!DOCTYPE html> <html lang='fr'> <head> <title>test</title> <meta charset='utf-8'> <style> body {background-color: black; color: red; text-align:center;} h1 {border: 15px red outset; margin-left:auto; margin-right: auto; width:1024px;} p {border: 5px red ridge; margin-left:auto; margin-right: auto; padding: 5px;width:1024px;} </style> </head> <body> <p>Ligne numéro %s</p> <p>Ligne numéro %s</p> <p>Ligne numéro %s</p> <p>Ligne numéro %s</p> <p>Ligne numéro %s</p> <p>Ligne numéro %s</p> <p>Ligne numéro %s</p> <p>Ligne numéro %s</p> </body> </html>''' print a % 'un','deux', 'trois', 'quatre', 'cinq', 'six', 'sept', 'huit' Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: not enough arguments for format string
Parfait sa marche seulement avec un fichier HTML plus conséquent sa bug en me disant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 a='''<!DOCTYPE html> <html lang='fr'> <head> <title>test</title> <meta charset='utf-8'> <style> body {background-color: black; color: red; text-align:center;} h1 {border: 15px red outset; margin-left:auto; margin-right: auto; width:1024px;} p {border: 5px red ridge; margin-left:auto; margin-right: auto; padding: 5px;width:1024px;} </style> </head> <body> <p>Ligne numéro %(un)s</p> <p>Ligne numéro %(deux)s</p> <p>Ligne numéro %(trois)s</p> <p>Ligne numéro %(quatre)s</p> <p>Ligne numéro %(cinq)s</p> <p>Ligne numéro %(six)s</p> <p>Ligne numéro %(sept)s</p> <p>Ligne numéro %(huit)s</p> </body> </html>''' print a % {'un':'un','deux':'deux', 'trois':'trois', 'quatre': 'quatre', 'cinq':'cinq', 'six': 'six', 'sept':'sept', 'huit':'huit'} <!DOCTYPE html> <html lang='fr'> <head> <title>test</title> <meta charset='utf-8'> <style> body {background-color: black; color: red; text-align:center;} h1 {border: 15px red outset; margin-left:auto; margin-right: auto; width:1024px;} p {border: 5px red ridge; margin-left:auto; margin-right: auto; padding: 5px;width:1024px;} </style> </head> <body> <p>Ligne numéro un</p> <p>Ligne numéro deux</p> <p>Ligne numéro trois</p> <p>Ligne numéro quatre</p> <p>Ligne numéro cinq</p> <p>Ligne numéro six</p> <p>Ligne numéro sept</p> <p>Ligne numéro huit</p> </body> </html>
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: not enough arguments for format string
On peut essayer:
Ca marche aussi mais même traceback pour mon fichier HTML plus conséquent:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 un='un' deux='deux' trois='trois' quatre='quatre' cinq='cinq' six='six' sept='sept' huit='huit' a='''<!DOCTYPE html> <html lang='fr'> <head> <title>test</title> <meta charset='utf-8'> <style> body {background-color: black; color: red; text-align:center;} h1 {border: 15px red outset; margin-left:auto; margin-right: auto; width:1024px;} p {border: 5px red ridge; margin-left:auto; margin-right: auto; padding: 5px;width:1024px;} </style> </head> <body> <p>Ligne numéro %(un)s</p> <p>Ligne numéro %(deux)s</p> <p>Ligne numéro %(trois)s</p> <p>Ligne numéro %(quatre)s</p> <p>Ligne numéro %(cinq)s</p> <p>Ligne numéro %(six)s</p> <p>Ligne numéro %(sept)s</p> <p>Ligne numéro %(huit)s</p> </body> </html>''' print a % vars() <!DOCTYPE html> <html lang='fr'> <head> <title>test</title> <meta charset='utf-8'> <style> body {background-color: black; color: red; text-align:center;} h1 {border: 15px red outset; margin-left:auto; margin-right: auto; width:1024px;} p {border: 5px red ridge; margin-left:auto; margin-right: auto; padding: 5px;width:1024px;} </style> </head> <body> <p>Ligne numéro un</p> <p>Ligne numéro deux</p> <p>Ligne numéro trois</p> <p>Ligne numéro quatre</p> <p>Ligne numéro cinq</p> <p>Ligne numéro six</p> <p>Ligne numéro sept</p> <p>Ligne numéro huit</p> </body> </html>
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: not enough arguments for format string
Je suppose que dans mon fichier HTML il y a d'autres signe '%' représentant des pourcentage afin de définir la taille d'un objet HTML qui sont interpréter par python comme des variables du type:
%(variable)s
Et je ne sais pas comment m'en sortir si quelqu'un comprend mon problème et a une solution a me donner il serai sympa de poster, ou si quelqu'un connait un module afin d'incorporer de manière dynamique des données texte dans un fichier HTML il serai sympa de poster, ou connait une autre technique de formatage il serai également sympa de poster, bref toutes, suggestion, commentaire, critique et solution sont la bienvenue.
Sur ce problème bon python a vous.
Merci pour vos réponses.
PS: pour info je tourne en 2.6.5 et non pas en python 3 qui dispose également, pour information, d'un module python pour apache.
Partager