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

82 views Asked by At

How can i avoid extra spaces in the tabulate grid?

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

gives me 2 extra spaces in every cell

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

how can i solve it to get

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

There are 2 answers

1
Antoine Delia On BEST ANSWER

You can leverage the MIN_PADDING constant.

import tabulate

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

tabulate.MIN_PADDING = 0

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

Outputs:

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

There is a global minimum padding setting you can change. I don't know how recommended that is but they mention it for the whitespace handling.

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