Listing 09-08: Generating a web page from a dictionary

##############################################################################
# Python From Scratch
# Autor: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2024
# Site: https://pythonfromscratch.com
#
# File: listing\chapter 09\09.08 - Generating a web page from a dictionary.py
# Description: Generating a web page from a dictionary
##############################################################################

movies = {
    "drama": ["Citizen Kane", "The Godfather"],
    "comedy": ["Modern Times", "American Pie", "Dr. Dolittle"],
    "police": ["Black Rain", "Desire to Kill", "Hard to Kill"],
    "war": ["Rambo", "Platoon", "Tora!Tora!Tora!"]
}
with open("films.html", "w", encoding="utf-8") as page:
    page.write("""
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Movies</title>
</head>
<body>
""")
    for c, v in films.items():
        page.write(f"<h1>{c}</h1>\n")
        for e in v:
            page.write(f"<h2>{e}</h2>\n")
    page.write("</body></html>")
Click here to download the file