Formatting Output

Chris cwitts at gmail.com
Mon Jun 2 04:38:01 EDT 2008


On Jun 2, 9:34 am, "victor.hera... at gmail.com"
<victor.hera... at gmail.com> wrote:
> Hi,
>
> i am building a little script and i want to output a series of columns
> more or less like this:
>
> 1  5  6
> 2  2  8
> 2  9  5
>
> The matter is that i don't know in advance how many columns there will
> be. By the way, each column will be actually FLOATs, not INTs. How can
> i do this ? Any help welcome. Regards,
>
> Victor

import sys
float_list = [1.0, 5.0, 6.0, 2.0, 2.0, 8.0, 2.0, 9.0, 5.0]
num_columns = 3
for i,item in enumerate(float_list):
    sys.stdout.write('%.4f\t' % item)
    if not (i+1) % num_columns:
        sys.stdout.write('\n')

Problem with this approach is it doesn't cater for instances where you
exceed the standard 80 characters for a terminal window.



More information about the Python-list mailing list