![python-tutorial-Python-courses-python-exercises-convert-dictionary-to-html-table](https://1.bp.blogspot.com/-NA7a1e5zM9k/X3L62HQM02I/AAAAAAAAB4s/EC1bM40HfmExjfcc6TJ7I7PvyaVP1lsMQCLcBGAsYHQ/s16000/python-courses.png)
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:
![](https://1.bp.blogspot.com/-CTdUnc24oc0/X3g2JQR31KI/AAAAAAAAB44/Hse9csFzX3U02Zf1QvtdgwsEUL4KXW9GgCLcBGAsYHQ/s16000/exerecise-python-convert-python-dictionary-to-html-table.png)
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 + "
Name
Email
{} {}
"
file = open("convert_dictionary.html" , 'w')
file.write(htmlContent)
file.close()
os.startfile("convert_dictionary.html")
Younes Derfoufi
my-courses.net
my-courses.net