【问题标题】:Python and HTML Producing TablesPython 和 HTML 生成表
【发布时间】:2017-05-20 21:43:24
【问题描述】:

我在尝试使用 python 和 html 生成两列时遇到问题。所以我只希望它只有两列和任意数量的行。这是我正在尝试制作的视觉效果:

这是我的python代码:

file_name = 'test.html'

test_file = open('test.html', 'w')

test_file.write('''<!DOCTYPE html>
<html>
    <head>
        <title> Dogs </title>
    </head>
    <body> 
        <table width="500" border="2" cellpadding="5">
''')

dog_names = ['Bob', 'Kenny', 'Robin', 'Ben', 'Tom', 'Steve']

dog_amount = len(dog_names)
index = 0

while dog_amount > index:
    name = dog_names[index]
    test_file.write('''           <tr>
                <td align="center" valign="center">
                    <p>''' + name + '''</p>
                </td>
            </tr>\n''')
    index += 1

test_file.write('''        </table>
    </body>
</html> ''')

这是在 html 文件中生成的内容:

<!DOCTYPE html>
<html>
    <head>
        <title> Dogs </title>
    </head>
    <body> 
        <table width="500" border="2" cellpadding="5">
           <tr>
                <td align="center" valign="center">
                    <p>Bob</p>
                </td>
            </tr>
           <tr>
                <td align="center" valign="center">
                    <p>Kenny</p>
                </td>
            </tr>
           <tr>
                <td align="center" valign="center">
                    <p>Robin</p>
                </td>
            </tr>
           <tr>
                <td align="center" valign="center">
                    <p>Ben</p>
                </td>
            </tr>
           <tr>
                <td align="center" valign="center">
                    <p>Tom</p>
                </td>
            </tr>
           <tr>
                <td align="center" valign="center">
                    <p>Steve</p>
                </td>
            </tr>
        </table>
    </body>
</html> 

【问题讨论】:

    标签: python html html-table


    【解决方案1】:

    我认为这应该可行。

    while dog_amount > index:
        test_file.write('''           <tr>
                    <td align="center" valign="center">
                        <p>''' + dog_names[index]+ '''</p>
                    </td><td align="center" valign="center">
                        <p>''' + dog_names[index+1]+ '''</p>
                    </td>
    
                </tr>\n''')
        index += 2
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      • 2010-12-05
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多