R. Weert
R. Weert

Reputation: 13

Different column length PrettyTable

Is it possible to get different lengths in column's in prettytable? When I try to use a list in PrettyTable I get the error: column length's do not match. Because one list has more items in it then the other list.

Example:

ListA = ("111", "222")
ListB = ("333")

PrettyTable:

t = Prettytable([])
t.add_column('Test1', ListA)
t.add_column('Test2', ListB)
print(t)

Upvotes: 1

Views: 2919

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

workaround this problem using zip_longest and a fill value, wrapped in zip again to add title:

import itertools

titles = ('Test1','Test2')
ListA = ("111", "222")
ListB = ("333",)

t = Prettytable([])

for title,lst in zip(titles,itertools.zip_longest(ListA,ListB,fillvalue="")):
    t.add_column(title,lst)

that will generate a sequence of the length longest list, padded with empty strings for shorter lists (and as a bonus you use a loop and not multiple add_column calls)

Upvotes: 1

Related Questions