python HTML for循环格式 [英] python HTML for loop format

查看:515
本文介绍了python HTML for循环格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下面的代码中将其格式化为HTML.但是我在将其格式化为HTML时遇到问题.

I want to make the code below to get formatted as HTML. However I'm having problem formatting it as HTML.

cur.execute("SELECT Statement")

rows = cur.fetchall()

    html = """\
    <table border='1'>
    <tr><th>Date</th><th>DPC</th><th>TOTAL</th><th>Success</th></tr>
    <tr>
    for row in rows:
        for col in row:
            print "<td>"
            print "%s".replace(" ", "") % col
            print "</td>"
            print "</tr>"
        </table> 
    """

我的目标是将其输出为html,但是我不知道如何将for循环输出为格式化的html.

My goal is to make it html output, however I don't know how to make the for loop output as formatted html.

推荐答案

我希望这是您要查找的代码:

I hope this is the code you're looking for:

html = """\
    <table border='1'>
    <tr><th>Date</th><th>DPC</th><th>TOTAL</th><th>Success</th></tr>"""
for row in rows:
    html = html + "<tr>"
    for col in row:
        html = html + "<td>" + col.replace(" ", "") + "</td>"
    html = html + "</tr>"
html = html + "</table>"

每个HTML标记都附加到html字符串变量.您可以将此html变量的内容写入文件:

Every HTML tag is appended to the html string variable. You can write the contents of this html variable to file:

f = open('temp.html','w')
f.write(html)
f.close()

这篇关于python HTML for循环格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆