Skip to content Skip to sidebar Skip to footer

Python Html For Loop Format

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 = '''\

Solution 1:

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

html = """\
    <tableborder='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>"

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()

Solution 2:

For that case you'll need to use jinja2.

<!DOCTYPE html><htmllang="en"><head>{{ title }}</head><body><tableborder='1'><thead><tr><th>Date</th><th>DPC</th><th>TOTAL</th><th>Success</th></tr></thead><tbody><tr>
          {% for col in rows %}
              <td>{{ col }}</td>
          {% endfor %}
       </tr></tbody></body></html>

Post a Comment for "Python Html For Loop Format"