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

Kent Johnson kent37 at tds.net
Sat Jul 14 17:19:04 EDT 2007


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. 
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'

Kent


More information about the Python-talk mailing list