0

所以我在表格中制作了一个表格,输出如下所示:

在此处输入图像描述

但我希望它看起来像这样:

在此处输入图像描述

那么我该怎么做呢?

这是我的代码:

from tabulate import tabulate

nums = {"Numbers": [0, 3.5, " ", -2, " "], "Seconds": [" ", " ", 24, " ", 3]}
print(
    tabulate(nums, tablefmt="grid", headers=nums.keys(), colalign=("center", "center"))
)

我将不胜感激,谢谢。

4

2 回答 2

1

您可以尝试以下输入:

nums = [["Numbers", 0, 3.5, " ", -2, " "], ["Seconds", " ", " ", 24, " ", 3]]

并通过删除headers=nums.keys()论点?

编辑:如果您的输入是您显示的字典,您可以将其转换为上面的列表列表:

from tabulate import tabulate
nums = {"Numbers": [0, 3.5, " ", -2, " "], "Seconds": [" ", " ", 24, " ", 3]}
print(
    tabulate([[k]+nums[k] for k in nums], tablefmt="grid", colalign=("center", "center"))
)
于 2020-11-25T16:34:52.467 回答
0

将字典转换为

nums = [["Numbers", 0, 3.5, " ", -2, " "], ["Seconds", " ", " ", 24, " ", 3]]

您可以使用:

nums = {"Numbers": [0, 3.5, " ", -2, " "], "Seconds": [" ", " ", 24, " ", 3]}

col = len(nums["Numbers"])
rd = len(nums)
l = []
ln = []
for k, v in nums.items():
    l = l + [k]
    l = l + v
    ln = ln + [l]
    l = []

print(ln)
于 2020-11-25T18:06:50.193 回答