jazzband / prettytable

Display tabular data in a visually appealing ASCII table format
https://pypi.org/project/PrettyTable/
Other
1.35k stars 154 forks source link

`add_divider` #242

Open tdegeus opened 1 year ago

tdegeus commented 1 year ago

Is your feature request related to a problem? Please describe.

I would like to avoid detecting the last item of a loop to add a divider. Therefore I would like to be able to set it separately, see example.

Describe the solution you'd like

import prettytable

tab = prettytable.PrettyTable()

for s in ["a", "b", "c"]:
    tab.add_row([s, s])

tab.add_divider()

for s in ["1", "2", "3"]:
    tab.add_row([s, s])

print(tab.get_string())

Describe alternatives you've considered

import prettytable

tab = prettytable.PrettyTable()

rows = ["a", "b", "c"]
for i, s in enumerate(rows):
    tab.add_row([s, s], divider=i == len(rows) - 1)

rows = ["1", "2", "3"]
for i, s in enumerate(rows):
    tab.add_row([s, s], divider=i == len(rows) - 1)

print(tab.get_string())

Additional context

-

viniciusarruda commented 1 year ago

Looking the source code, the solution is:

import prettytable

tab = prettytable.PrettyTable()

for s in ["a", "b", "c"]:
    tab.add_row([s, s])

#tab.add_divider()
if len(tab._dividers) > 0:
    tab._dividers[-1] = True

for s in ["1", "2", "3"]:
    tab.add_row([s, s])

print(tab.get_string())

Be aware that if code changes, this can break.