python - 如何使用 python 在 HTML 文件中打印嵌套列表?
问题描述
我正在尝试使用 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>
解决方案
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
<table>
for word in words:
myFile.write(f'<tr><td>{word}</td></tr>')
</table>
myFile.write('</body>')
myFile.write('</html>')
我试图编辑接受的答案,但我不可用,但你只需要添加<table> and </table>
推荐阅读
- machine-learning -
运行中:无法解析 IO 错误类型为 13 的传入对象 - python - 如何在设定值周围插入带有填充/边界的信号?〜Python,熊猫,scipy.interpolate
- javascript - 模糊侦听器的回调无法识别状态值
- python - 得到误报,误报,......带有混乱包的指标
- c# - 通用渲染管道 - 尽管在质量设置中分配了 URP 资产,但相机错误
- google-apps-script - 新的 Google Drive 资源关键要求是否会影响 Google Apps 对电子表格的访问
- python - 如何仅使用 Python 源代码编辑器在 Houdini 中创建动画书?
- ios - 当键盘出现时,有什么方法可以修改 SwiftUI List 的 contentOffset 吗?
- playwright - 如何在 Typescript 中使用 Playwright 和 Playwright/test 实现页面对象模型?
- dns - 等待肯定响应的 dnsmasq 并行查询