Solution Exercice 80: algorithme python qui convertis un dictionnaire en une table html

Exercice80

Écrire un programme en python qui transforme un dictionnaire en une table html et enregistre le contenu en sur un fichier html nommé 'convert_dictionary.html'. Exemple pour le dictionnaire:

name_email = {'david' : 'david@gmail.com' , 'hafid' : 'hafid@gmail.com' , 'nathalie' : 'nathalie@gmail.com' , 'najib' : 'najib@gmail.com' }

le programme doit créer un fichier un fichier html qui affiche le contenu dans une table html:

Solution




#coding: utf-8
import os
name_email = {'david' : 'david@gmail.com' ,  'hafid' : 'hafid@gmail.com' ,  'nathalie' : 'nathalie@gmail.com' , 'najib' : 'najib@gmail.com' }

htmlContent = """
<table border = '2' style = " border-collapse:collapse; " width = 320>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
"""
for key , value in name_email.items():
    htmlContent = htmlContent + "<tr><td>{}</td> <td>{}</td> </tr>".format(key , value)
htmlContent = htmlContent + "</tbody></table>"
file = open("convert_dictionary.html" , 'w')
file.write(htmlContent)
file.close()
os.startfile("convert_dictionary.html")




 

Younes Derfoufi
CRMEF OUJDA

1 thought on “Solution Exercice 80: algorithme python qui convertis un dictionnaire en une table html

Leave a Reply

Your email address will not be published. Required fields are marked *