mylist = [ ( ('12', '47', '4', '574862', '58', '7856'), 'AGGREGATE_VALUE1'),
( ('2', '75', '757', '8233', '838', '47775272785'), 'AGGREG2'),
( ('4144', '78', '78965', '778', '78578', '2'), 'AGGREGATE_VALUE3')]
longg = dict.fromkeys((0,1,2,3,4,5,6),0)
for tu,x in mylist:
for i,el in enumerate(tu):
longg[i] = max(longg[i],len(str(el)))
longg[6] = max(longg[6],len(str(x)))
fofo = ' '.join('%'+str(longg[i])+'s' for i in xrange(0,7))
print '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist)
result
12 47 4 574862 58 7856 AGGREGATE_VALUE1
2 75 757 8233 838 47775272785 AGGREG2
4144 78 78965 778 78578 2 AGGREGATE_VALUE3
Don't know if this fills your need
EDIT 1
Using string formatting with modulo operator (%) to print in a constant length, '%6s' right-justifies in a constant length of 6, and '%-6s' left-justifies in a constant length of 6.
You'll find precisions here
But there is no sense to specify a constant length to print something at the end of a string, because it's somewhat naturally-left-justified in this case.
Then :
longg = dict.fromkeys((0,1,2,3,4,5,),0)
for tu,x in mylist:
for i,el in enumerate(tu):
longg[i] = max(longg[i],len(str(el)))
fofo = ' '.join('%'+str(longg[i])+'s' for i in xrange(0,6)) + ' %s'
print '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist)
EDIT 2
mylist = [ ( (12, 47, 4, 574862, 58, 7856), 'AGGREGATE_VALUE1'),
( (2, 75, 757, 8233, 838, 47775272785), 'AGGREG2'),
( (4144, 78, 78965, 778, 78578, 2), 'AGGREGATE_VALUE3')]
longg = dict.fromkeys((0,1,2,3,4,5),0)
for tu,_ in mylist:
longg.update(( i, max(longg[i],len(str(el))) ) for i,el in enumerate(tu))
fofo = ' '.join('%%%ss' % longg[i] for i in xrange(0,6)) + ' %s'
print '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist)
EDIT 3
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)))
result
Price1 | Price2 | reference | XYD | code | resp | AGGREG values
-------|--------|-----------|--------|-------|-------------|-----------------
12 | 47 | 4 | 574862 | 58 | 7856 | AGGREGATE_VALUE1
2 | 75 | 757 | 8233 | 838 | 47775272785 | AGGREG2
4144 | 78 | 78965 | 778 | 78578 | 2 | AGGREGATE_VALUE3
Note that this kind of formatting would be much easier with the string's method format() introduced in Python 2.6