python-tabulate icon indicating copy to clipboard operation
python-tabulate copied to clipboard

Incorrect row width when using SEPARATING_LINE with some table formats

Open AnjoMan opened this issue 3 years ago • 1 comments

When i use SEPARATING_LINE in 'mixed_grid' or 'simple_outline', i get an extra row (according to the docs, this is how the separator is done for some styles). However, i noticed the extra row does not seem to adopt the width of the table, but just has a small string surrounded by the exterior borders.

An MWE:

from tabulate import tabulate, SEPARATING_LINE
mse_table = tabulate(
    [
        ["Mean-Squared Current Error", "∑ΔI² / N",  "0.00000000000000000032486767"  + " A²"],
        ["Mean-Squared Voltage Error", "∑ΔV² / N",  "0.00000000000000000000000019092038" + " V p.u.²"],
        SEPARATING_LINE,
        ["Mean-Absolute Current Error", "∑|ΔI| / N", "0.00000000021306265" + " A"],
        ["Mean-Absolute Voltage Error", "∑|ΔV| / N", "0.000000000000341719" + " V p.u."]
    ],
    headers=["Summarizing Error", "Formula", "Value"],
    tablefmt="mixed_grid"
)

print(mse_table)

This produces the following table:

┌─────────────────────────────┬───────────┬────────────────────────────────────────────┐
│ Summarizing Error           │ Formula   │ Value                                      │
├─────────────────────────────┼───────────┼────────────────────────────────────────────┤
│ Mean-Squared Current Error  │ ∑ΔI² / N  │ 0.00000000000000000032486767 A²            │
│ Mean-Squared Voltage Error  │ ∑ΔV² / N  │ 0.00000000000000000000000019092038 V p.u.² │
│  │
│ Mean-Absolute Current Error │ ∑|ΔI| / N │ 0.00000000021306265 A                      │
│ Mean-Absolute Voltage Error │ ∑|ΔV| / N │ 0.000000000000341719 V p.u.                │
└─────────────────────────────┴───────────┴────────────────────────────────────────────┘

It seems to me like there should ideally be a step that computes the width of the row, or injects it a bit more formally in the table so its width is properly derived from whatever tabulate picks for the rest of the table. If i use [""] instead of SEPARATING_LINE then i get the behaviour i would expect.

Observed this on tabulate==0.9.0

AnjoMan avatar Dec 19 '22 17:12 AnjoMan

If instead of printing at the end, you just dump the resulting string, you will see that SEPARATING_LINE is just \x01, meaning that it is somehow not being converted to the correct value.

It is instantiated on line 49: https://github.com/astanin/python-tabulate/blob/537d7b03932263062d37a7e747f19a385709b9f7/tabulate/init.py#L49

The true separator is inserted in: https://github.com/astanin/python-tabulate/blob/537d7b03932263062d37a7e747f19a385709b9f7/tabulate/init.py#L2634-L2649

Additional testing shows that this happens for any format that has padding, but any format without padding properly creates a separating line.

jevandezande avatar Dec 03 '24 23:12 jevandezande