I am trying to write a HTML file using python, and I want to print in .html a nested list.
I have written this, but I don´t have any idea about how to doit good.
JavaScript
x
14
14
1
words = [['Hi'], ['From'], ['Python']]
2
3
with open('mypage.html', 'w') as myFile:
4
myFile.write('<html>')
5
myFile.write('<body>')
6
myFile.write('<h1>---------------------------</h1>')
7
8
for i in range(len(words)):
9
myFile.write('<tr><td>'(words[i])'</td></tr>')
10
11
12
myFile.write('</body>')
13
myFile.write('</html>')
14
In .html I want to print the nested list in a table in this similar format:
JavaScript
1
14
14
1
<body>
2
<table>
3
<tr>
4
<td>Hi</td>
5
</tr>
6
<tr>
7
<td>From</td>
8
</tr>
9
<tr>
10
<td>Python</td>
11
</tr>
12
</table>
13
</body>
14
Advertisement
Answer
JavaScript
1
21
21
1
words = [['Hi'], ['From'], ['Python']]
2
3
with open('mypage.html', 'w') as myFile:
4
myFile.write('<html>')
5
myFile.write('<body>')
6
myFile.write('<h1>---------------------------</h1>')
7
8
9
# 2-depth string data to 1-depth
10
words = [word_str for inner in words for word_str in inner]
11
12
# use fstring to build string
13
<table>
14
for word in words:
15
myFile.write(f'<tr><td>{word}</td></tr>')
16
</table>
17
18
19
myFile.write('</body>')
20
myFile.write('</html>')
21
I tried to edit the accepted answer but I were not available but, you just have to add <table> and </table>