python-tutorial-Python-courses-python-exercises-convert-dictionary-to-html-table

Exercise 120

Write a python program that transforms a dictionary into an html table and saves the content to an html file called 'convert_dictionary.html'. Example for the dictionary:

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

the program should create an html file that displays the content in an html table: 

Solution

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

htmlContent = """

"""
for key , value in name_email.items():
htmlContent = htmlContent + "".format(key , value)
htmlContent = htmlContent + "






NameEmail
{} {}
"
file = open("convert_dictionary.html" , 'w')
file.write(htmlContent)
file.close()
os.startfile("convert_dictionary.html")

Younes Derfoufi
my-courses.net

Leave a Reply