json生成html表格
约 125 字
预计阅读 1 分钟
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
name | age | sex |
---|
Jack | 25 | male |
Rebot | 18 | male |