如何使用python在HTML文件中打印嵌套列表?

爱德华多·埃雷拉(Eduardo Herrera)

我正在尝试使用python编写HTML文件,并且我想在.html中打印嵌套列表。

我已经写了这篇文章,但是我对如何做好事一无所知。

words = [['Hi'], ['From'], ['Python']]

with open('mypage.html', 'w') as myFile:
    myFile.write('<html>')
    myFile.write('<body>')
    myFile.write('<h1>---------------------------</h1>')

    for i in range(len(words)):
        myFile.write('<tr><td>'(words[i])'</td></tr>')


    myFile.write('</body>')
    myFile.write('</html>')

在.html中,我想以类似的格式在表格中打印嵌套列表:

<body>
    <table>
        <tr>
            <td>Hi</td>
        </tr>
        <tr>
            <td>From</td>
        </tr>
        <tr>
            <td>Python</td>
        </tr>
    </table>
</body>
城ony公园

这个怎么样?

words = [['Hi'], ['From'], ['Python']]

with open('mypage.html', 'w') as myFile:
    myFile.write('<html>')
    myFile.write('<body>')
    myFile.write('<h1>---------------------------</h1>')

    
    # 2-depth string data to 1-depth 
    words = [word_str for inner in words for word_str in inner] 
    
    # use fstring to build string
    for word in words:
        myFile.write(f'<tr><td>{word}</td></tr>')  


    myFile.write('</body>')
    myFile.write('</html>')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 Django 在 HTML 文件中打印列表

如何使用嵌套循环打印嵌套列表?

如何使用python中的嵌套循环从列表中指定次数打印所有字符串

如何使用嵌套循环在python中打印两个列表的索引

如何使用python计算嵌套列表中字符串的0并打印其索引

如何使用 Python 将列表中的嵌套 json 存储到文本文件中?

如何使用 Javascript 打印地图的嵌套列表

如何在Python中的exec中使用嵌套%打印格式?

如何使用Python在嵌套字典中打印特定的整数变量?

如何使用python中的列表打印int列表

如何使用python中的列表创建嵌套列表?

如何使用 Python 函数对嵌套列表中的每个列表求和?

使用for循环从Python中的外部文件打印列表的每个项目

如何使用Python从Json文件的嵌套列表中提取对象?

使用文件内容在python中创建嵌套列表

使用 jQuery/JavaScript 在 HTML 页面中显示/打印一组嵌套的 JSON API 数据(列表)

如何使用golang打印HTML文件中的特定內容?

如何在Python 3.6中根据需要打印一组嵌套列表

如何在python中打印具有最高总和的嵌套对象列表

如何使用python查找和替换嵌套列表中的字符?

在Python中,如何使用for循环从字典创建嵌套列表?

如何在python中的嵌套列表上正确使用递归?

如何使用for循环遍历Python中的嵌套列表

如何在Python中使用不同的数据类型打印嵌套列表的元素?

逐行打印嵌套列表-Python

在OCaml中打印嵌套列表

使用 python 列表打印到 HTML 表

从嵌套的 python 字典中显示 HTML 列表

如何使用python从文件中打印字典的值