[Python-talk] Module to massage text file for better output?

Lloyd Kvam python at venix.com
Sun Jul 15 22:36:15 EDT 2007


On Sat, 2007-07-14 at 17:19 -0400, Kent Johnson wrote:
> Lloyd Kvam wrote:
> > So you'd build the outfmt string(s) in the first pass and produce the
> > pretty report in the second pass.  (double the % to escape it)
> >         outfmt = '%%%ds,%%%ds,%%%ds' % (35,35,20)
> >         produces '%35s,%35s,%20s'
> 
> There is no need to produce the output format string in code like this. 

Yes it is ugly but...

> If you use a * as the field width, the width is taken from the format 
> arguments:
> 
> In [3]: '%*s' % (35, 'foo')
> Out[3]: '                                foo'
> In [4]: '%*s' % (5, 'foo')
> Out[4]: '  foo'

Combining lengths and values into an alternating list isn't always so
great.  Also that combining gets done for every line of input versus
building the output format just once.

> 
> Kent
-- 
Lloyd Kvam
Venix Corp



More information about the Python-talk mailing list