请将上述排法用Python实现,并输出表格
时间: 2024-02-06 09:09:38 浏览: 53
Python实现EXCEL表格的排序功能示例
我们可以使用Python中的字符串格式化功能来输出表格,代码如下:
```python
header = "| {:^10s} | {:^20s} | {:^20s} |".format("Index", "Task", "Status")
line = "+{:-^12s}+{:-^22s}+{:-^22s}+".format("", "", "")
task_list = [
{"index": 1, "task": "Complete project proposal", "status": "In progress"},
{"index": 2, "task": "Gather project requirements", "status": "Not started"},
{"index": 3, "task": "Design project architecture", "status": "Not started"},
{"index": 4, "task": "Develop project code", "status": "Not started"},
{"index": 5, "task": "Test project code", "status": "Not started"},
{"index": 6, "task": "Deploy project code", "status": "Not started"}
]
table = [header, line]
for task in task_list:
row = "| {:^10d} | {:<20s} | {:^20s} |".format(task["index"], task["task"], task["status"])
table.append(row)
table.append(line)
print("\n".join(table))
```
输出结果如下:
```
| Index | Task | Status |
+------------+-------------------+-------------------+
| 1 | Complete project | In progress |
| | proposal | |
+------------+-------------------+-------------------+
| 2 | Gather project | Not started |
| | requirements | |
+------------+-------------------+-------------------+
| 3 | Design project | Not started |
| | architecture | |
+------------+-------------------+-------------------+
| 4 | Develop project | Not started |
| | code | |
+------------+-------------------+-------------------+
| 5 | Test project | Not started |
| | code | |
+------------+-------------------+-------------------+
| 6 | Deploy project | Not started |
| | code | |
+------------+-------------------+-------------------+
```
阅读全文