python函数,为包含两个单元格的行创建HTML

时间:2011-03-27 18:41:52

标签: python html

我正在寻找在python中编写一个函数,它接受一个字典和一个单独的键作为参数,以便为包含两个单元格的行创建HTML。我希望单元格包含字典中的键/值对。

字典 -

d = {'year':1999, 'director':'Mike Judge', 'title':'Office Space'}

我猜测函数的外观;

def makeHTMLRow(d, 'title'):
s = <table border ="1">\n
s = s + "<tr>\n"
s = s + "\t<td> row 1, cell 1</td>\n"
s = s + "\t<td> row 1, cell 2</td>\n"
s = s + "</tr>"
s = s + "<tr>\n"
s = s + "\t<td> row 2, cell 1</td>\n"

这是正确的方向吗?

我想在Google App Engine上使用我的Python代码,这就是我用Python编写HTML的原因。

2 个答案:

答案 0 :(得分:2)

您编写的代码没问题,但您可以使其更简洁:

def makeHTMLRow(d, 'title'):
    s =  "<tr>\n\t"
    s += "<td>%(year)d</td>\n\t"
    s += "<td>%(director)s</td>\n"
    s += "</tr><tr>\n\t"
    s += "<td>%(title)s></td>\n"
    s += "</tr>"
    return s % d

此示例使用字典的python字符串格式。

答案 1 :(得分:0)

问题是什么?如果您无权在托管环境中安装任意代码,则必须自己编写。或者您重复使用现有的模块,如

http://www.pasko.net/PyHtmlTable/

(Google是你的朋友)

相关问题