[英]How to print a nested list in a HTML file using python?
I am trying to write a HTML file using python, and I want to print in.html a nested list.我正在尝试使用 python 编写 HTML 文件,并且我想在.html 中打印一个嵌套列表。
I have written this, but I don´t have any idea about how to doit good.我已经写了这个,但我不知道如何做好。
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>')
In.html I want to print the nested list in a table in this similar format: In.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>')
I tried to edit the accepted answer but I were not available but, you just have to add <table> and </table>
我试图编辑接受的答案,但我不可用,但您只需添加<table> and </table>
How about this?这个怎么样?
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>')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.