json生成html表格

  1. demo
  2. preview

demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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表格

文章字数:183

本文作者:Spaceack

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

最后更新:2020-02-14, 14:52:46

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

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

目录