python 制表表fmt=rounded_outline列之间打印 3 个空格而不是 1 个空格

python tabulate tablefmt=rounded_outline prints 3 spaces instead of 1 space between columns

提问人:gsxr1300 提问时间:11/18/2023 更新时间:11/18/2023 访问量:33

问:

如何避免表格网格中多余的空格?

rows = [
    ["A1", "B2"],
    ["C3", "D4"],
    ["E5", "E6"],
]
print(tabulate(rows, headers="firstrow", tablefmt='rounded_outline'))

在每个单元格中给我 2 个额外的空格

╭──────┬──────╮
│ A1   │ B2   │
├──────┼──────┤
│ C3   │ D4   │
│ E5   │ E6   │
╰──────┴──────╯

我怎样才能解决它来获得

╭────┬────╮
│ A1 │ B2 │
├────┼────┤
│ C3 │ D4 │
│ E5 │ E6 │
╰────┴────╯
Python 制表

评论


答:

0赞 Holloway 11/18/2023 #1

您可以更改全局最小填充设置。我不知道这是如何推荐的,但他们提到了空格处理。

import tabulate
tabulate.MIN_PADDING = 0
rows = [
    ["A1", "B2"],
    ["C3", "D4"],
    ["E5", "E6"],
]
print(tabulate.tabulate(rows, headers="firstrow", tablefmt='rounded_outline'))
0赞 Antoine Delia 11/18/2023 #2

您可以利用常量。MIN_PADDING

import tabulate

rows = [
    ["A1", "B2"],
    ["C3", "D4"],
    ["E5", "E6"],
]

tabulate.MIN_PADDING = 0

print(tabulate.tabulate(rows, headers="firstrow", tablefmt="rounded_outline"))

输出:

╭────┬────╮
│ A1 │ B2 │
├────┼────┤
│ C3 │ D4 │
│ E5 │ E6 │
╰────┴────╯

评论

0赞 gsxr1300 11/18/2023
哇,效果很好:-)