Question: How do I format printing in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joemacbusiness@yahoo.com

    Question: How do I format printing in python

    Hi All,

    How do I format printed data in python?
    I could not find this in the Python Reference Manual:

    Nor could I find it in Matloff's great tutorial:



    For example, how do I turn this:

    512 Jun 5 2004 X11r6
    22 Jan 17 2005 a2p
    22 Jan 17 2005 acctcom
    5374 Sep 15 2002 acledit
    5664 May 13 2004 aclget
    12020 May 13 2004 aclput
    115734 Jun 2 2004 adb
    46518 Jun 4 2004 admin
    66750 Sep 16 2002 ali
    1453 Sep 15 2002 alias
    28150 Jun 4 2004 alog
    15 May 12 2005 alstat

    into this:

    512 Jun 5 2004 X11r6
    22 Jan 17 2005 a2p
    22 Jan 17 2005 acctcom
    5374 Sep 15 2002 acledit
    5664 May 13 2004 aclget
    12020 May 13 2004 aclput
    115734 Jun 2 2004 adb
    46518 Jun 4 2004 admin
    66750 Sep 16 2002 ali
    1453 Sep 15 2002 alias
    28150 Jun 4 2004 alog
    15 May 12 2005 alstat

    Thank you
  • Saul Spatz

    #2
    Re: Question: How do I format printing in python

    format the strings:




    joemacbusiness@ yahoo.com wrote:
    Hi All,
    >
    How do I format printed data in python?
    I could not find this in the Python Reference Manual:

    Nor could I find it in Matloff's great tutorial:

    >
    >
    For example, how do I turn this:
    >
    512 Jun 5 2004 X11r6
    22 Jan 17 2005 a2p
    22 Jan 17 2005 acctcom
    5374 Sep 15 2002 acledit
    5664 May 13 2004 aclget
    12020 May 13 2004 aclput
    115734 Jun 2 2004 adb
    46518 Jun 4 2004 admin
    66750 Sep 16 2002 ali
    1453 Sep 15 2002 alias
    28150 Jun 4 2004 alog
    15 May 12 2005 alstat
    >
    into this:
    >
    512 Jun 5 2004 X11r6
    22 Jan 17 2005 a2p
    22 Jan 17 2005 acctcom
    5374 Sep 15 2002 acledit
    5664 May 13 2004 aclget
    12020 May 13 2004 aclput
    115734 Jun 2 2004 adb
    46518 Jun 4 2004 admin
    66750 Sep 16 2002 ali
    1453 Sep 15 2002 alias
    28150 Jun 4 2004 alog
    15 May 12 2005 alstat
    >
    Thank you

    Comment

    • Mensanator

      #3
      Re: Question: How do I format printing in python

      On Jun 23, 12:12 pm, joemacbusin...@ yahoo.com wrote:
      Hi All,
      >
      How do I format printed data in python?
      I could not find this in the Python Reference Manual:http://docs.python.org/ref/print.html
      Nor could I find it in Matloff's great tutorial:http://heather.cs.ucdavis.edu/~matlo...ythonIntro.pdf
      >
      For example, how do I turn this:
      >
      512 Jun 5 2004 X11r6
      22 Jan 17 2005 a2p
      22 Jan 17 2005 acctcom
      5374 Sep 15 2002 acledit
      5664 May 13 2004 aclget
      12020 May 13 2004 aclput
      115734 Jun 2 2004 adb
      46518 Jun 4 2004 admin
      66750 Sep 16 2002 ali
      1453 Sep 15 2002 alias
      28150 Jun 4 2004 alog
      15 May 12 2005 alstat
      >
      into this:
      >
      512        Jun   5   2004    X11r6
      22         Jan   17  2005    a2p
      22         Jan   17  2005    acctcom
      5374       Sep   15  2002    acledit
      5664       May   13  2004    aclget
      12020      May   13  2004    aclput
      115734     Jun   2   2004    adb
      46518      Jun   4   2004    admin
      66750      Sep   16  2002    ali
      1453       Sep   15  2002    alias
      28150      Jun   4   2004    alog
      15         May   12  2005    alstat
      >
      Thank you

      You could do this:

      data = ['512 Jun 5 2004 X11r6 ', \
      '22 Jan 17 2005 a2p', \
      '22 Jan 17 2005 acctcom ', \
      '5374 Sep 15 2002 acledit ', \
      '5664 May 13 2004 aclget ', \
      '12020 May 13 2004 aclput ', \
      '115734 Jun 2 2004 adb ', \
      '46518 Jun 4 2004 admin ', \
      '66750 Sep 16 2002 ali ', \
      '1453 Sep 15 2002 alias ', \
      '28150 Jun 4 2004 alog ', \
      '15 May 12 2005 alstat ']

      for i in data:
      d = i.split()
      print d[0].ljust(9),
      print d[1].ljust(6),
      print d[2].ljust(4),
      print d[3].ljust(7),
      print d[4].ljust(9)


      which gives you

      512 Jun 5 2004 X11r6
      22 Jan 17 2005 a2p
      22 Jan 17 2005 acctcom
      5374 Sep 15 2002 acledit
      5664 May 13 2004 aclget
      12020 May 13 2004 aclput
      115734 Jun 2 2004 adb
      46518 Jun 4 2004 admin
      66750 Sep 16 2002 ali
      1453 Sep 15 2002 alias
      28150 Jun 4 2004 alog
      15 May 12 2005 alstat


      or perhaps this:

      for i in data:
      d = i.split()
      print d[0].rjust(9),
      print d[1].ljust(6),
      print d[2].zfill(2).ljust (4),
      print d[3].ljust(7),
      print d[4].ljust(9)

      which gives this (if you want the digits in the numbers to
      line up):

      512 Jun 05 2004 X11r6
      22 Jan 17 2005 a2p
      22 Jan 17 2005 acctcom
      5374 Sep 15 2002 acledit
      5664 May 13 2004 aclget
      12020 May 13 2004 aclput
      115734 Jun 02 2004 adb
      46518 Jun 04 2004 admin
      66750 Sep 16 2002 ali
      1453 Sep 15 2002 alias
      28150 Jun 04 2004 alog
      15 May 12 2005 alstat

      Comment

      • Mensanator

        #4
        Re: Question: How do I format printing in python

        On Jun 23, 12:47 pm, Mensanator <mensana...@aol .comwrote:
        On Jun 23, 12:12 pm, joemacbusin...@ yahoo.com wrote:
        >
        >
        >
        >
        >
        Hi All,
        >
        How do I format printed data in python?
        I could not find this in the Python Reference Manual:http://docs.python..org/ref/print.html
        Nor could I find it in Matloff's great tutorial:http://heather.cs.ucdavis.edu/~matlo...ythonIntro.pdf
        >
        For example, how do I turn this:
        >
        512 Jun 5 2004 X11r6
        22 Jan 17 2005 a2p
        22 Jan 17 2005 acctcom
        5374 Sep 15 2002 acledit
        5664 May 13 2004 aclget
        12020 May 13 2004 aclput
        115734 Jun 2 2004 adb
        46518 Jun 4 2004 admin
        66750 Sep 16 2002 ali
        1453 Sep 15 2002 alias
        28150 Jun 4 2004 alog
        15 May 12 2005 alstat
        >
        into this:
        >
        512        Jun   5   2004    X11r6
        22         Jan   17  2005    a2p
        22         Jan   17  2005    acctcom
        5374       Sep   15  2002    acledit
        5664       May   13  2004    aclget
        12020      May   13  2004    aclput
        115734     Jun   2   2004    adb
        46518      Jun   4   2004    admin
        66750      Sep   16  2002    ali
        1453       Sep   15  2002    alias
        28150      Jun   4   2004    alog
        15         May   12  2005    alstat
        >
        Thank you
        >
        You could do this:
        >
        data = ['512 Jun 5 2004 X11r6 ', \
        '22 Jan 17 2005 a2p', \
        '22 Jan 17 2005 acctcom ', \
        '5374 Sep 15 2002 acledit ', \
        '5664 May 13 2004 aclget ', \
        '12020 May 13 2004 aclput ', \
        '115734 Jun 2 2004 adb ', \
        '46518 Jun 4 2004 admin ', \
        '66750 Sep 16 2002 ali ', \
        '1453 Sep 15 2002 alias ', \
        '28150 Jun 4 2004 alog ', \
        '15 May 12 2005 alstat ']
        >
        for i in data:
          d = i.split()
          print d[0].ljust(9),
          print d[1].ljust(6),
          print d[2].ljust(4),
          print d[3].ljust(7),
          print d[4].ljust(9)
        >
        which gives you
        >
        512       Jun    5    2004    X11r6
        22        Jan    17   2005    a2p
        22        Jan    17   2005    acctcom
        5374      Sep    15   2002    acledit
        5664      May    13   2004    aclget
        12020     May    13   2004    aclput
        115734    Jun    2    2004    adb
        46518     Jun    4    2004    admin
        66750     Sep    16   2002    ali
        1453      Sep    15   2002    alias
        28150     Jun    4    2004    alog
        15        May    12   2005    alstat
        >
        or perhaps this:
        >
        for i in data:
          d = i.split()
          print d[0].rjust(9),
          print d[1].ljust(6),
          print d[2].zfill(2).ljust (4),
          print d[3].ljust(7),
          print d[4].ljust(9)
        >
        which gives this (if you want the digits in the numbers to
        line up):
        >
              512 Jun    05   2004    X11r6
               22 Jan    17   2005    a2p
               22 Jan    17   2005    acctcom
             5374 Sep    15   2002    acledit
             5664 May    13   2004    aclget
            12020 May    13   2004    aclput
           115734 Jun    02   2004    adb
            46518 Jun    04   2004    admin
            66750 Sep    16   2002    ali
             1453 Sep    15   2002    alias
            28150 Jun    04   2004    alog
               15 May    12   2005    alstat
        In retrospect, that looks kind of clunky. If it was for my
        use, I'd do

        for i in data:
        d = i.split()
        print d[0].rjust(9),
        print d[1].rjust(6),
        print d[2].zfill(2),
        print d[3].ljust(7),
        print d[4].ljust(9)

        which looks like:

        512 Jun 05 2004 X11r6
        22 Jan 17 2005 a2p
        22 Jan 17 2005 acctcom
        5374 Sep 15 2002 acledit
        5664 May 13 2004 aclget
        12020 May 13 2004 aclput
        115734 Jun 02 2004 adb
        46518 Jun 04 2004 admin
        66750 Sep 16 2002 ali
        1453 Sep 15 2002 alias
        28150 Jun 04 2004 alog
        15 May 12 2005 alstat

        Comment

        • Mark Tolonen

          #5
          Re: Question: How do I format printing in python


          <joemacbusiness @yahoo.comwrote in message
          news:c6e1cfa9-207a-461b-babe-2a14cdd1b25a@y2 1g2000hsf.googl egroups.com...
          Hi All,
          >
          How do I format printed data in python?
          I could not find this in the Python Reference Manual:

          Nor could I find it in Matloff's great tutorial:

          >
          >
          For example, how do I turn this:
          >
          512 Jun 5 2004 X11r6
          22 Jan 17 2005 a2p
          22 Jan 17 2005 acctcom
          5374 Sep 15 2002 acledit
          5664 May 13 2004 aclget
          12020 May 13 2004 aclput
          115734 Jun 2 2004 adb
          46518 Jun 4 2004 admin
          66750 Sep 16 2002 ali
          1453 Sep 15 2002 alias
          28150 Jun 4 2004 alog
          15 May 12 2005 alstat
          >
          into this:
          >
          512 Jun 5 2004 X11r6
          22 Jan 17 2005 a2p
          22 Jan 17 2005 acctcom
          5374 Sep 15 2002 acledit
          5664 May 13 2004 aclget
          12020 May 13 2004 aclput
          115734 Jun 2 2004 adb
          46518 Jun 4 2004 admin
          66750 Sep 16 2002 ali
          1453 Sep 15 2002 alias
          28150 Jun 4 2004 alog
          15 May 12 2005 alstat
          >
          Thank you
          data = '''\
          512 Jun 5 2004 X11r6
          22 Jan 17 2005 a2p
          22 Jan 17 2005 acctcom
          5374 Sep 15 2002 acledit
          5664 May 13 2004 aclget
          12020 May 13 2004 aclput
          115734 Jun 2 2004 adb
          46518 Jun 4 2004 admin
          66750 Sep 16 2002 ali
          1453 Sep 15 2002 alias
          28150 Jun 4 2004 alog
          15 May 12 2005 alstat
          '''.split('\n')

          for line in data:
          elements = line.split()
          print '%-11s%-6s%-4s%-8s%s' % tuple(elements)

          -Mark

          Comment

          • Larry Bates

            #6
            Re: Question: How do I format printing in python

            joemacbusiness@ yahoo.com wrote:
            Hi All,
            >
            How do I format printed data in python?
            I could not find this in the Python Reference Manual:

            Nor could I find it in Matloff's great tutorial:

            >
            >
            For example, how do I turn this:
            >
            512 Jun 5 2004 X11r6
            22 Jan 17 2005 a2p
            22 Jan 17 2005 acctcom
            5374 Sep 15 2002 acledit
            5664 May 13 2004 aclget
            12020 May 13 2004 aclput
            115734 Jun 2 2004 adb
            46518 Jun 4 2004 admin
            66750 Sep 16 2002 ali
            1453 Sep 15 2002 alias
            28150 Jun 4 2004 alog
            15 May 12 2005 alstat
            >
            into this:
            >
            512 Jun 5 2004 X11r6
            22 Jan 17 2005 a2p
            22 Jan 17 2005 acctcom
            5374 Sep 15 2002 acledit
            5664 May 13 2004 aclget
            12020 May 13 2004 aclput
            115734 Jun 2 2004 adb
            46518 Jun 4 2004 admin
            66750 Sep 16 2002 ali
            1453 Sep 15 2002 alias
            28150 Jun 4 2004 alog
            15 May 12 2005 alstat
            >
            Thank you
            data = '''512 Jun 5 2004 X11r6
            22 Jan 17 2005 a2p
            22 Jan 17 2005 acctcom
            5374 Sep 15 2002 acledit
            5664 May 13 2004 aclget
            12020 May 13 2004 aclput
            115734 Jun 2 2004 adb
            46518 Jun 4 2004 admin
            66750 Sep 16 2002 ali
            1453 Sep 15 2002 alias
            28150 Jun 4 2004 alog
            15 May 12 2005 alstat
            '''

            for entry in data.rstrip().s plit('\n'):
            items = entry.split(' ')
            print "%-6s %3s %2s %4s %-7s" % tuple(items)

            Comment

            • Maric Michaud

              #7
              Re: Question: How do I format printing in python

              Le Tuesday 24 June 2008 05:33:01 Larry Bates, vous avez écrit :...
              >
              data = '''512 Jun 5 2004 X11r6
              22 Jan 17 2005 a2p
              22 Jan 17 2005 acctcom
              5374 Sep 15 2002 acledit
              5664 May 13 2004 aclget
              12020 May 13 2004 aclput
              115734 Jun 2 2004 adb
              46518 Jun 4 2004 admin
              66750 Sep 16 2002 ali
              1453 Sep 15 2002 alias
              28150 Jun 4 2004 alog
              15 May 12 2005 alstat
              '''
              >
              for entry in data.rstrip().s plit('\n'):
              items = entry.split(' ')
              print "%-6s %3s %2s %4s %-7s" % tuple(items)
              In python, str objects have a splitlines method for portability it is better,
              a shorthand for split(os.sep).


              --
              _____________

              Maric Michaud

              Comment

              • Lie

                #8
                Re: Question: How do I format printing in python

                On Jun 24, 12:12 am, joemacbusin...@ yahoo.com wrote:
                Hi All,
                >
                How do I format printed data in python?
                I could not find this in the Python Reference Manual:http://docs.python.org/ref/print.html
                Nor could I find it in Matloff's great tutorial:http://heather.cs.ucdavis..edu/~matl...ythonIntro.pdf
                >
                For example, how do I turn this:
                >
                512 Jun 5 2004 X11r6
                22 Jan 17 2005 a2p
                22 Jan 17 2005 acctcom
                5374 Sep 15 2002 acledit
                5664 May 13 2004 aclget
                12020 May 13 2004 aclput
                115734 Jun 2 2004 adb
                46518 Jun 4 2004 admin
                66750 Sep 16 2002 ali
                1453 Sep 15 2002 alias
                28150 Jun 4 2004 alog
                15 May 12 2005 alstat
                >
                into this:
                >
                512        Jun   5   2004    X11r6
                22         Jan   17  2005    a2p
                22         Jan   17  2005    acctcom
                5374       Sep   15  2002    acledit
                5664       May   13  2004    aclget
                12020      May   13  2004    aclput
                115734     Jun   2   2004    adb
                46518      Jun   4   2004    admin
                66750      Sep   16  2002    ali
                1453       Sep   15  2002    alias
                28150      Jun   4   2004    alog
                15         May   12  2005    alstat
                >
                Thank you
                There is string formatting

                print formatspecifier _string % data_sequence
                The format specifier is similar to the one used in C's printf, and
                data sequence may be tuple or list. Dictionary may also be used for
                data, but it has its own way to specify string formatting since
                dictionary is unordered but "indexed" by the dict key.

                Comment

                • Donald 'Paddy' McCarthy

                  #9
                  Re: Question: How do I format printing in python

                  Lie wrote:
                  On Jun 24, 12:12 am, joemacbusin...@ yahoo.com wrote:
                  >Hi All,
                  >>
                  >How do I format printed data in python?
                  >I could not find this in the Python Reference Manual:http://docs.python.org/ref/print.html
                  >Nor could I find it in Matloff's great tutorial:http://heather.cs.ucdavis.edu/~matlo...ythonIntro.pdf
                  >>
                  >For example, how do I turn this:
                  >>
                  >512 Jun 5 2004 X11r6
                  >22 Jan 17 2005 a2p
                  >22 Jan 17 2005 acctcom
                  >5374 Sep 15 2002 acledit
                  >5664 May 13 2004 aclget
                  >12020 May 13 2004 aclput
                  >115734 Jun 2 2004 adb
                  >46518 Jun 4 2004 admin
                  >66750 Sep 16 2002 ali
                  >1453 Sep 15 2002 alias
                  >28150 Jun 4 2004 alog
                  >15 May 12 2005 alstat
                  >>
                  >into this:
                  >>
                  >512 Jun 5 2004 X11r6
                  >22 Jan 17 2005 a2p
                  >22 Jan 17 2005 acctcom
                  >5374 Sep 15 2002 acledit
                  >5664 May 13 2004 aclget
                  >12020 May 13 2004 aclput
                  >115734 Jun 2 2004 adb
                  >46518 Jun 4 2004 admin
                  >66750 Sep 16 2002 ali
                  >1453 Sep 15 2002 alias
                  >28150 Jun 4 2004 alog
                  >15 May 12 2005 alstat
                  >>
                  >Thank you
                  >
                  There is string formatting
                  >
                  print formatspecifier _string % data_sequence
                  The format specifier is similar to the one used in C's printf, and
                  data sequence may be tuple or list. Dictionary may also be used for
                  data, but it has its own way to specify string formatting since
                  dictionary is unordered but "indexed" by the dict key.
                  I have attached a prog I wrote to answer someones elses similar problem.

                  - Paddy.



                  from StringIO import StringIO
                  from pprint import pprint as pp
                  left_justified = False
                  debug = False

                  textinfile = '''I$created$a$ solution$for$a$ program$I$am$wr iting$that
                  makes$columns$l ine$up$when$out putted$to$the$c ommand
                  line.$I$am$new$ to$Python,$and$ am$hoping$I$mig ht$get
                  some$input$on$t his$topic.$In$t he$text$file$th at$the
                  program$reads,$ the$fields$(col umns)$are$delim ited$by
                  'dollar'$and$th e$records$(line s)$are$delimite d$by
                  newlines$'\\n'. $So$one$line$lo oks$like:$'''

                  """

                  Solution to problem posed at:


                  Output is the following if left-justified:

                  # Column-aligned output:
                  I created a solution for a program I am writing that
                  makes columns line up when outputted to the command
                  line. I am new to Python, and am hoping I might get
                  some input on this topic. In the text file that the
                  program reads, the fields (columns) are delimited by
                  'dollar' and the records (lines) are delimited by
                  newlines '\n'. So one line looks like:

                  And like this if not left-justified:

                  # Column-aligned output:
                  I created a solution for a program I am writing that
                  makes columns line up when outputted to the command
                  line. I am new to Python, and am hoping I might get
                  some input on this topic. In the text file that the
                  program reads, the fields (columns) are delimited by
                  'dollar' and the records (lines) are delimited by
                  newlines '\n'. So one line looks like:

                  """

                  infile = StringIO(textin file)

                  fieldsbyrecord= [line.strip().sp lit('$') for line in infile]
                  if debug: print "fieldsbyrecord :"; print (fieldsbyrecord )
                  # pad to same number of fields per record
                  maxfields = max(len(record) for record in fieldsbyrecord)
                  fieldsbyrecord = [fields + ['']*(maxfields - len(fields))
                  for fields in fieldsbyrecord]
                  if debug: print "padded fieldsbyrecord: "; print (fieldsbyrecord )
                  # rotate
                  fieldsbycolumn = zip(*fieldsbyre cord)
                  if debug: print "fieldsbycolumn :"; print (fieldsbycolumn )
                  # calculate max fieldwidth per column
                  colwidths = [max(len(field) for field in column)
                  for column in fieldsbycolumn]
                  if debug: print "colwidths: "; print (colwidths)
                  # pad fields in columns to colwidth with spaces
                  # (Use -width for left justification,)
                  fieldsbycolumn = [ ["%*s" % (-width if left_justified else width, field) for field in column]
                  for width, column in zip(colwidths, fieldsbycolumn) ]
                  if debug: print "padded fieldsbycolumn: "; print (fieldsbycolumn )
                  # rotate again
                  fieldsbyrecord = zip(*fieldsbyco lumn)
                  if debug: print "padded rows and fields, fieldsbyrecord: "; print (fieldsbyrecord )
                  # printit
                  print "\n# Column-aligned output:"
                  for record in fieldsbyrecord:
                  print " ".join(reco rd)



                  Comment

                  Working...