我正在尝试使用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
for word in words:
myFile.write(f'<tr><td>{word}</td></tr>')
myFile.write('</body>')
myFile.write('</html>')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句