如何在python中编写和保存html文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16523939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:55:12  来源:igfitidea点击:

How to write and save html file in python?

python

提问by Erika Sawajiri

This is what I know how to write and save 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>

采纳答案by Anubhav C

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 存储在一个字符串中并将该字符串传递给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()

回答by GWW

print('<tr><td>%04d</td>' % (i+1), file=Html_file)

回答by Igo Coelho

You can try:

你可以试试:

colour = ["red", "red", "green", "yellow"]

with open('mypage.html', 'w') as myFile:
    myFile.write('<html>')
    myFile.write('<body>')
    myFile.write('<table>')

    s = '1234567890'
    for i in range(0, len(s), 60):
        myFile.write('<tr><td>%04d</td>' % (i+1));
    for j, k in enumerate(s[i:i+60]):
        myFile.write('<td><font style="background-color:%s;">%s<font></td>' % (colour[j %len(colour)], k));


    myFile.write('</tr>')
    myFile.write('</table>')
    myFile.write('</body>')
    myFile.write('</html>')

回答by Nurul Akter Towhid

You can do it using write():

您可以使用write()来做到这一点

#open file with *.html* extension to write html
file= open("my.html","w")
#write then close file
file.write(html)
file.close()

回答by Greenstick

You can also do this without having to call close()using the withkeyword. For example:

您也可以这样做而无需close()使用with关键字进行调用。例如:

# HTML String
html = """
<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>
"""

# Write HTML String to file.html
with open("file.html", "w") as file:
    file.write(html)

See https://stackoverflow.com/a/11783672/2206251for more details on the withkeyword in Python.

有关Python 中关键字的更多详细信息,请参阅https://stackoverflow.com/a/11783672/2206251with

回答by mousomer

shorter version of Nurul Akter Towhid's answer (the fp.close is automated):

Nurul Akter Towhid 答案的较短版本(fp.close 是自动的):

with open("my.html","w") as fp:
   fp.write(html)