h函数渲染table
时间: 2024-01-22 21:19:25 浏览: 8
假设你有一个包含数据的列表,可以使用以下代码渲染一个HTML表格:
```python
def render_table(data):
# 创建表格
table = "<table>"
# 创建表头
table += "<tr>"
for header in data[0]:
table += "<th>{}</th>".format(header)
table += "</tr>"
# 创建表格数据
for row in data[1:]:
table += "<tr>"
for cell in row:
table += "<td>{}</td>".format(cell)
table += "</tr>"
# 结束表格
table += "</table>"
# 返回渲染后的HTML
return table
```
此函数将数据作为参数传入,然后创建表格,包括表头和数据行。最后将HTML表格字符串返回。你可以像这样使用它:
```python
data = [
["Name", "Age", "Gender"],
["Alice", "25", "Female"],
["Bob", "30", "Male"],
["Charlie", "40", "Male"]
]
table_html = render_table(data)
print(table_html)
```
输出:
```html
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
</tr>
<tr>
<td>Alice</td>
<td>25</td>
<td>Female</td>
</tr>
<tr>
<td>Bob</td>
<td>30</td>
<td>Male</td>
</tr>
<tr>
<td>Charlie</td>
<td>40</td>
<td>Male</td>
</tr>
</table>
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)