Skip to content Skip to sidebar Skip to footer

Python Pretty Printing A List In A Tabular Format

I am referring to a post here Pretty printing a list in a tabular format mylist = [ ( (12, 47, 4, 574862, 58, 7856), 'AGGREGATE_VALUE1'), ( (2, 75, 757, 8233, 838, 47775

Solution 1:

The error is that range1 is a string while you expect to have variables. You perhaps may use something like eval?

So you can do:

'\n'.join(fofo % eval(range1) for (a,b,c,d,e,f),g in mylist)

Or to avoid refering to several vars:

'\n'.join(fofo % (tuple(x[0])+(x[1],)) for x in mylist)

Each element of mylist is made by a tuple containing a tuple and a string like ((a,b..), "string"). So if you consider x containing such data, tuple(x[0]) designate the first tuple while (x[1],) make a tuple with single element that is the string. Finally tuple(x[0])+(x[1],) will create only one tuple with all elements like:

    x = ((0, 1, 2), "a")
    tuple(x[0])+(x[1],)  #is equivalent to (0, 1, 2, "a")

This will work with ((0, 1, 2, 3, 4), "a"), or any number of element in the inside tuple.

Post a Comment for "Python Pretty Printing A List In A Tabular Format"