json生成html表格

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
文章作者: Spaceack
文章链接: http://spaceack.com/2019/12/11/2019-12-11-json%E7%94%9F%E6%88%90html%E8%A1%A8%E6%A0%BC/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 丸子家的小云吞
支付宝打赏
微信打赏