Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_divider #242

Open
tdegeus opened this issue May 11, 2023 · 1 comment
Open

add_divider #242

tdegeus opened this issue May 11, 2023 · 1 comment

Comments

@tdegeus
Copy link

tdegeus commented May 11, 2023

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants