json生成html表格

  1. demo
  2. preview

demo

data = {"column_name":["name", "age", "sex"],
        "column": [["Jack", "25", "male"],
                   ["Rebot","18", "male"]]}

col_ks = data.get("column_name")
col_vs = data.get("column")

def dict_to_table(ks, vs):
"""
desc: dict2html_table
"""
    th = ''
    for name in ks:
        th = th + '<th>' + name + '</th>'
    trth = '<tr>' + th + '</tr>'

    trtd = ''
    for tds in vs:
        tdss = ''
        for td in tds:
            tdss = tdss + '<td>' + str(td) + '</td>'
        tdss = '<tr>' + tdss + '</tr>'
        trtd = trtd + tdss

    return '<table>' + trth + trtd + '</table>'

print(dict_to_table(col_ks, col_vs))
# <table><tr><th>name</th><th>age</th><th>sex</th></tr><tr><td>Jack</td><td>25</td><td>male</td></tr><tr><td>Rebot</td><td>18</td><td>male</td></tr></table>

preview

nameagesex
Jack25male
Rebot18male

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 [email protected]

文章标题:json生成html表格

文章字数:239

本文作者:Spaceack

发布时间:2019-12-11, 04:00:00

最后更新:2020-01-21, 03:13:31

原始链接:http://spaceack.com/2019/12/11/2019-12-11-python-code-shippet-json2table/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录