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

maxcolwidths isn't properly respected: extra whitespace is added

Open yurivict opened this issue 1 year ago • 0 comments

Testcase:

from tabulate import tabulate

headers = ['Header#1', 'Header#2', 'Header#3']
data = []
data.append(['Alpha beta gama zeta omega', 'The weather was exceptionally good that day again', 'The files were concatenated and archived for posterity.'])
data.append(['Delta omega beta alpha nu and the rest', 'The weather was exceptionally good that day', 'They decided to engage in many businesses and all of them were successful.'])

print(tabulate(
    data,
    headers=headers,
    tablefmt='fancy_grid',
    maxcolwidths=[None, None, 8]))

The output:

╒════════════════════════════════════════╤═══════════════════════════════════════════════════╤════════════╕
│ Header#1                               │ Header#2                                          │ Header#3   │
╞════════════════════════════════════════╪═══════════════════════════════════════════════════╪════════════╡
│ Alpha beta gama zeta omega             │ The weather was exceptionally good that day again │ The        │
│                                        │                                                   │ files      │
│                                        │                                                   │ were con   │
│                                        │                                                   │ catenate   │
│                                        │                                                   │ d and      │
│                                        │                                                   │ archived   │
│                                        │                                                   │ for post   │
│                                        │                                                   │ erity.     │
├────────────────────────────────────────┼───────────────────────────────────────────────────┼────────────┤
│ Delta omega beta alpha nu and the rest │ The weather was exceptionally good that day       │ They       │
│                                        │                                                   │ decided    │
│                                        │                                                   │ to         │
│                                        │                                                   │ engage     │
│                                        │                                                   │ in many    │
│                                        │                                                   │ business   │
│                                        │                                                   │ es and     │
│                                        │                                                   │ all of     │
│                                        │                                                   │ them       │
│                                        │                                                   │ were suc   │
│                                        │                                                   │ cessful.   │
╘════════════════════════════════════════╧═══════════════════════════════════════════════════╧════════════╛

The last column in the output has 2 extra spaces. The data was wrapped at the requested column width of 8, but then 2 extra spaces were added.

This is important because maxcolwidths can be (and is in my case) calculated based on the actual terminal width, and adding extra space makes the table to overflow the terminal.

Version: 0.9.0 Python 3.11 (3.9 works the same way)

yurivict avatar Nov 14 '24 20:11 yurivict