Issue
This is what I know about writing to an HTML file and saving it:
html_file = open("filename","w")
html_file.write()
html_file.close()
But how do I save to the file if I want to write a really long codes like this:
1 <table border=1>
2 <tr>
3 <th>Number</th>
4 <th>Square</th>
5 </tr>
6 <indent>
7 <% for i in range(10): %>
8 <tr>
9 <td><%= i %></td>
10 <td><%= i**2 %></td>
11 </tr>
12 </indent>
13 </table>
Solution
You can create multi-line strings by enclosing them in triple quotes. So you can store your HTML in a string and pass that string to write()
:
html_str = """
<table border=1>
<tr>
<th>Number</th>
<th>Square</th>
</tr>
<indent>
<% for i in range(10): %>
<tr>
<td><%= i %></td>
<td><%= i**2 %></td>
</tr>
</indent>
</table>
"""
Html_file= open("filename","w")
Html_file.write(html_str)
Html_file.close()
Answered By – Anubhav C
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0