0

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, 47775272785), 'AGGREG2'),
           ( (4144, 78, 78965, 778, 78578, 2), 'AGGREGATE_VALUE3')]

header = ('Price1','Price2','reference','XYD','code','resp','AGGREG values')

longg = dict(zip((0,1,2,3,4,5,6),(len(str(x)) for x in header)))

for tu,x in mylist:
    longg.update(( i, max(longg[i],len(str(el))) ) for i,el in enumerate(tu))
    longg[6] = max(longg[6],len(str(x)))
fofo = ' | '.join('%%-%ss' % longg[i] for i in xrange(0,7))

print '\n'.join((fofo % header,
                 '-|-'.join( longg[i]*'-' for i in xrange(7)),
                 '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist)))

I want to replace '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist))) with something dynamic. For example range1 = ', '.join(map(chr, range(97, 97+7))) which gives me a,b,c,d,e,f,g

so the line will look '\n'.join(fofo % (range1) for (a,b,c,d,e,f),g in mylist)))

but gives me:

TypeError: not enough arguments for format string

Community
  • 1
  • 1
Thomas
  • 3
  • 1

1 Answers1

0

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.

daouzli
  • 15,288
  • 1
  • 18
  • 17
  • eval worked. How do I pass the same in the for loop. \n'.join(fofo % eval(range1) for (range2),g in mylist) ?? – Thomas May 01 '14 at 20:38
  • range1 = ', '.join(map(chr, range(97, 97+7))) range2 = ', '.join(map(chr, range(98, 97+7))) '\n'.join(fofo % (a, b, c, d, e, f, g) for a,(b,c,d,e,f,g) in myList))) — works '\n'.join(fofo % (eval(range1)) for a,(range2) in myList))) — does not work - error NameError: name 'b' is not defined '\n'.join(fofo % (eval(range1)) for a,(eval(range2)) in myList))) - does not work - error SyntaxError: can't assign to function call – Thomas May 01 '14 at 20:45
  • That cannot work as the vars you want to evaluate don't exist. It is not the same case than in range1 – daouzli May 01 '14 at 21:02
  • how can I do this? I am trying to dynamically trying to set the range so I can use as long as I know the count of header elements and avoid hard coding it. Thanks Thomas – Thomas May 01 '14 at 21:15
  • I can't see an other way than something like: '\n'.join(fofo % (tuple(x[0])+(x[1],)) for x in mylist) – daouzli May 01 '14 at 21:18
  • \n'.join(fofo % (a, b, c, d, e, f, g) for a,(b,c,d,e,f,g) in myList))) '\n'.join(fofo % (eval(range1)) for a,(b,c,d,e,f,g) in myList))) above two works How can I replace (b,c,d,e,f,g) based on some count – Thomas May 01 '14 at 21:19
  • see just before my last answer – daouzli May 01 '14 at 21:29
  • '\n'.join(fofo % (tuple(x[0])+(x[1],)) for x in mylist) – daouzli May 01 '14 at 21:35
  • I need it to be dynamic. Your last answer I did not understand. The tuple need to be dynamically build based on some count? The count is number of elements in the header it needs to print. Thanks Thomas – Thomas May 01 '14 at 21:55