json生成html表格

2020-11-04 14:24:52 浏览数 (1)

demo

代码语言:javascript复制
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

0 人点赞