python中的HTML编码:如何自动生成表格?

时间:2015-07-03 18:07:48

标签: python html

我有

  fruits = [ "apple" , "pineapple" , "banana", "oranges"] 
  sizes = ["small", "medium", "large"]

我想创建一个HTML主页。

为此我现在正在这样做:

import os

def home_html(func)
    htmlFile = open('home_fruits.html', 'w')
    message = """<DOCType html>
             <html>
             <head>
             <title> Classification of fruits </title>
             </head> 
             <body> <h1> Fruits-Home Page </h1>
             <style type = "text/css">
             style code for the table 
             </style> 
             <table class = "tg">
             <tr>
             <th class="tg-031e">apple</th>
             <th class="tg-031e">banana</th>
             <th class="tg-031e">pineapple</th>
             <th class="tg-031e">oranges</th> 
             </tr> 
             <tr> 
             <td class="tg-031e"><a href = "apple_details.html"</th> Details for apple </a></td>
              similarly for pineapple 
              similarly for banana 
              similarly for oranges 
             </tr> 
             </table>
             </body>
             </html>"""

   htmlFile.write(message)
   htmlFile.close() 

同样,我创建了每个HTML页面,如apple_details.html等。所有硬编码。

有一些简单的方法可以做到这一点吗? 喜欢而不是

<th class="tg-031e">apple</th>
<td class="tg-031e"><a href = "apple_details.html"</th>

如果我这样做

<th class="tg-031e">fruit</th>

<td class="tg-031e"><a href = "fruit_details.html"</th>

应该为所有水果创建自动表吗?

问题不重复,不是要求模板生成。我想减少硬编码。

0 个答案:

没有答案