微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将带有字典的嵌套列表打印到 HTML 文件中?

如何解决如何将带有字典的嵌套列表打印到 HTML 文件中?

使用 products 我想自动生成这些表:

Tables

这些表的代码应该是:

<!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>

我所做的是:

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','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,...]
    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()

但我得到以下信息:

no tables

问题在于它没有放入表格中,我想将其放入表格中,然后使用 CSS 添加样式。 我希望你能帮助我,在此先感谢!

解决方法

这将在不使用模板的情况下为表格创建 HTML。

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','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,...]
    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()
,

这是我会做的:

在views.py中:

    def myMarket(request):

    products = [['Clean','description': 'A resistant door'}]]
    products_header = [i[0] for i in products] # get the first items "Clean,Kitchen,House"
    products_body = [i[1:] for i in products] # get the other items "The dicts"
    products_header_and_body = dict(zip(products_header,products_body)) # Convert them into key pairs

    context = {
        'products': products_header_and_body,}
           
    return render(request,'myMarket.html',context)

在 HTML 模板中:

    <!DOCTYPE html>
<body>
    <h1>Fun Market</h1>
    <table border="1">
        {% for k,v in products.items %}
        <thead bgcolor= "red">
            <th>{{k}}</th>
        </thead>
        {% for item in v %}
        <tbody>
            {% for s,t in item.items %}
            <tr>
                <td>{{s}}: <b style="color: blue">{{t}}</b></td>
            </tr>
            {% endfor %}
        </tbody>
        {% endfor %}

        {% endfor %}
        
</body>
</html>

输出:

enter image description here

您可以对 HTML 模板中的表格进行更多 css 样式设置。但是遍历列表所需要做的就是将其转换为密钥对。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?