gpt4 book ai didi

python - 使用 python 列表的 html 文档中的表

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:00 24 4
gpt4 key购买 nike

我在 python 中的 csv 文件中有一个名为 new_list 的嵌套数据列表,我需要将其放入 html 文档中的一个简单表格中。

[['Jason', 'Brown', 'Leeds', '40'], ['Sarah', 'Robinson', 'Bristol', '32'], ['Carlo', 'Baldi', '曼彻斯特', '41']]

我已经设法在 Python 控制台中为表格标题编写 html,但不知道如何引用列表中的内容 - 例如,在 <tr> 之间放置什么内容标签来填充行。这是我目前所拥有的:

display = open("table.html", 'w')
display.write("""<HTML>
<body>
<h1>Attendance list</h1>
<table>
<tr></tr>
<tr></tr>
</table>
</body>
</HTML>""")

非常感谢!

最佳答案

简单的字符串和列表操作。

html = """<HTML>
<body>
<h1>Attendance list</h1>
<table>
{0}
</table>
</body>
</HTML>"""

items = [['Jason', 'Brown', 'Leeds', '40'], ['Sarah', 'Robinson', 'Bristol', '32'], ['Carlo', 'Baldi', 'Manchester', '41']]
tr = "<tr>{0}</tr>"
td = "<td>{0}</td>"
subitems = [tr.format(''.join([td.format(a) for a in item])) for item in items]
# print html.format("".join(subitems)) # or write, whichever

输出:

<HTML>
<body>
<h1>Attendance list</h1>
<table>
<tr><td>Jason</td><td>Brown</td><td>Leeds</td><td>40</td></tr><tr><td>Sarah</td><td>Robinson</td><td>Bristol</td><td>32</td></tr><tr><td>Carlo</td><td>Baldi</td><td>Manchester</td><td>41</td></tr>
</table>
</body>
</HTML>

enter image description here

关于python - 使用 python 列表的 html 文档中的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33920896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com