Formatting Output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • victor.herasme@gmail.com

    Formatting Output

    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
  • Marc 'BlackJack' Rintsch

    #2
    Re: Formatting Output

    On Mon, 02 Jun 2008 00:34:09 -0700, victor.herasme@ gmail.com wrote:
    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,
    Look at string methods, `join()` for example, and string formatting with
    the ``%`` operator.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Chris

      #3
      Re: Formatting Output

      On Jun 2, 9:34 am, "victor.hera... @gmail.com"
      <victor.hera... @gmail.comwrote :
      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.writ e('%.4f\t' % item)
      if not (i+1) % num_columns:
      sys.stdout.writ e('\n')

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

      Comment

      Working...