How to print a nested list with dictionaries inside to an HTML file?

With products I want to generate automatically these tables: Tables

And code for these tables should be:

<!DOCTYPE html>
<body>
    <h1>Fun Market</h1>
    <table border="1">
        <thead bgcolor= "red">
            <th>Clean</th>
        </thead>
        <tbody>
            <tr>
                <td>Cleanex $ 01.00</td>
            </tr>
            <tr>
                <td>to clean</td>
            </tr>
            <tr>
                <td>Toothbrush $ 01.50</td>
            </tr>
            <tr>
                <td>oral hygiene</td>
            </tr>
        </tbody>
    </table>

    <table border="1">
        <thead bgcolor= "red">
            <th>Kitchen</th>
        </thead>
        <tbody>
            <tr>
                <td>Spaguetti $ 01.00</td>
            </tr>
            <tr>
                <td>Easy to cook</td>
            </tr>
            <tr>
                <td>Rice $ 1.00</td>
            </tr>
            <tr>
                <td>For all family</td>
            </tr>
        </tbody>
    </table>

    <table border="1">
        <thead bgcolor= "red">
            <th>House</th>
        </thead>
        <tbody>
            <tr>
                <td>Door $ 25.00</td>
            </tr>
            <tr>
                <td>A resistant door</td>
            </tr>
        </tbody>
    </table>

</body>
</html>

What I have done is this:

def textFile():
    products = [['Clean', {'number': '42565', 'name': 'Cleanex', 'price': '01.00', 'description': 'to clean'}, {'number': '45217', 'name': 'Toothbrush', 'price': '01.50', 'description': 'oral hygiene'}], 
                ['Kitchen', {'number': '47851', 'name': 'Spaguetti', 'price': '01.00', 'description': 'Easy to cook'}, {'number': '5852', 'name': 'Rice', 'price': '1.00', 'description': 'For all family'}], 
                ['House', {'number': '78595', 'name': 'Door', 'price': '25.00', 'description': 'A resistant door'}]]
    #If it helps, products has the following estructure: [[Category, dictionary1,dictionary2,...],[Category, dictionary1,dictionary2,...],[Category, dictionary1,dictionary2,...],...]
    sending = """
        <!DOCTYPE html>
    <body>
        <h3>Fun Market</h3>
        <hr>"""
    for number  in products:
        sending = sending + """<h1 >"""+number[0]+""" </h1>"""
        for i in range(1,len(number)):
            sending = sending + """<h3 >"""+number[i]['name']+" $ "+number[i]['price']+"""</h3>"""
            sending = sending + """<h4>"""+number[i]['description']+"""</h4>"""
    for i in range(4):
        sending = sending + """<h1> </h1>"""
        sendingAgain = """    
        </body>
        </html>
            """
        sending = sending + sendingAgain
        text = open("Store.html", "wb")
        text.write(bytes(sending,'utf-8'))
        text.close()
        
textFile()

But I get the following:

no tables

And the problem with that is that it is not into tables, and I would like to get it in tables to then add styles with CSS. I hope you can help me, thanks in advance!

Answer

This will create the HTML for the tables without using a template.

def textFile():
    products = [['Clean', {'number': '42565', 'name': 'Cleanex', 'price': '01.00', 'description': 'to clean'}, {'number': '45217', 'name': 'Toothbrush', 'price': '01.50', 'description': 'oral hygiene'}], 
                ['Kitchen', {'number': '47851', 'name': 'Spaguetti', 'price': '01.00', 'description': 'Easy to cook'}, {'number': '5852', 'name': 'Rice', 'price': '1.00', 'description': 'For all family'}], 
                ['House', {'number': '78595', 'name': 'Door', 'price': '25.00', 'description': 'A resistant door'}]]
    #If it helps, products has the following estructure: [[Category, dictionary1,dictionary2,...],[Category, dictionary1,dictionary2,...],[Category, dictionary1,dictionary2,...],...]
    sending = """
        <!DOCTYPE html>
    <body>
        <h3>Fun Market</h3>
        <hr>"""
    for number  in products:
      table = f'<table border="1"><thead bgcolor="red"><th>{number[0]}</th></thead>'
      table = table +"<tbody>"
      for i in range(1,len(number)):
        trow = f"<tr><td>{number[i]['name']+' '+number[i]['price']}</td></tr>"
        table += trow
        trow =  f"<tr><td>{number[i]['description']}</td></tr>"
        table += trow
      table += "</tbody></table>"
      sending +=table
    sendingAgain = """    
    </body>
    </html>
        """
    sending = sending + sendingAgain
    text = open("Store.html", "wb")
    text.write(bytes(sending,'utf-8'))
    text.close()
        
textFile()

Leave a Reply

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