如何使用对象写入html文件?

问题描述 投票:-1回答:1

免责声明,我对编码非常陌生,但是我需要做的是写入html文件并在使用类的同时使用python生成表。为了查看该类是否起作用,我创建了一个随机对象。该程序运行,但是当我打开html文件时,我看到的只是“测试”;没有生成表格如何解决?

class webtable:
    def __init__(self, st):
        self.st = st
        self.f = " "

    def htmlopen(self, st):
        self.f=open('test.html', 'w')

    def table(self, st):
        table = "<table style='width: 100%;'; border='1'>"
        return table 

    def tbody(self, st):
        tbody = "<tbody>"
        tbodyc = "</tbody>"
        return tbody + str(st) + tbodyc

    def tr(self, st):
        tr = "<tr>"
        trc = "</tr>"
        return tr + str(st) + trc

    def th(self, st):
        th = "<th>"
        thc = "</th>"
        return th + str(st) + thc

    def td(self, st):
        td = "<td>"
        tdc = "</td>"
        return td + str(st) + tdc

    def paragraph(self, st):
        para = "<p>"
        parac = "</p>"
        return para + str(st) + parac

    def h1(self, st):
        h1 = "<h1>"
        h1c = "</h1>"
        return h1 + str(st) + h1c

    def write(self, st):
        self.f.write(str(st))

st="test"
x1=webtable(st)
x1.htmlopen()
x1.table(st)
x1.tbody(st)
x1.tr(st)
x1.th(st)
x1.td(st)
x1.paragraph(st)
x1.h1(st)
x1.write(st)
python html class object writing
1个回答
0
投票

您可能需要一些模板系统。例如tornadoweb template

from tornado import template
t = template.Template("<html>{{ myvalue }}</html>")
print(t.generate(myvalue="XXX"))

使用循环语法,您可以呈现表的行或单元格。

© www.soinside.com 2019 - 2024. All rights reserved.