Separator in print statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bertram Scharpf

    Separator in print statement


    Hi,

    when I write
    [color=blue][color=green][color=darkred]
    >>> print 'abc', 'def',
    >>> print 'ghi'[/color][/color][/color]

    I get the output 'abc def ghi\n'.

    Is there a way to manipulate the print
    statment that I get for example:

    'abc, def, ghi\n'

    I mean: can I substitute the ' ' separator produced from
    the comma operator by a e.g. ', ' or something else?

    Thanks in advance.

    Bertram

    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
  • Peter Hansen

    #2
    Re: Separator in print statement

    Bertram Scharpf wrote:[color=blue]
    >
    > when I write
    >[color=green][color=darkred]
    > >>> print 'abc', 'def',
    > >>> print 'ghi'[/color][/color]
    >
    > I get the output 'abc def ghi\n'.
    >
    > Is there a way to manipulate the print
    > statment that I get for example:[/color]

    The general rule with "print" is that it works like it does, and
    if you don't like the way it works, you need to switch to something
    else.
    [color=blue]
    > 'abc, def, ghi\n'
    >
    > I mean: can I substitute the ' ' separator produced from
    > the comma operator by a e.g. ', ' or something else?[/color]

    If you require that the output be generated by separate statements
    or subroutine calls, then you will have to do something fairly
    complicated: create an object which acts like a file object, and
    which can collect blobs of data as you output them, but hold
    them in memory, writing them all out together after you send
    it the terminating sequence (\n in this case).

    A simpler option is just to collect up the bits of output that
    you need in a list, then use the string join() method to generate
    the output:

    outList = []
    outList.extend(['abc', 'def'])
    outList.append( 'ghi')
    print ', '.join(outList)

    I've included both the .extend() and .append() approaches, to
    most closely emulate your above example. You don't need to
    use .extend() if you don't want, and the code might be simpler
    if you don't.

    -Peter

    Comment

    • Matt Goodall

      #3
      Re: Separator in print statement

      Bertram Scharpf wrote:
      [color=blue]
      >Hi,
      >
      >when I write
      >[color=green][color=darkred]
      > >>> print 'abc', 'def',
      > >>> print 'ghi'[/color][/color]
      >
      >I get the output 'abc def ghi\n'.
      >
      >Is there a way to manipulate the print
      >statment that I get for example:
      >
      >'abc, def, ghi\n'
      >
      >I mean: can I substitute the ' ' separator produced from
      >the comma operator by a e.g. ', ' or something else?
      >[/color]

      print '%s, %s, %s' % ('abc', 'def', 'ghi')

      or better still

      print ', '.join(('abc', 'def', 'ghi'))

      If you want super-fine control then don't use print, use sys.stdout.writ e().

      Cheers, Matt

      --
      Matt Goodall, Pollenation Internet Ltd
      w: http://www.pollenationinternet.com
      e: matt@pollenatio n.net



      Comment

      • Bertram Scharpf

        #4
        Re: Separator in print statement

        Hi Peter,

        thank you for your detailed answer.

        Peter Hansen schrieb im Artikel <3F8C2522.8D172 9DD@engcorp.com >:[color=blue]
        > The general rule with "print" is that it works like it does, and
        > if you don't like the way it works, you need to switch to something
        > else.[/color]

        Anyway. I mean ' ' when I say ' '.
        [color=blue]
        > If you require that the output be generated by separate statements
        > or subroutine calls, then you will have to do something fairly
        > complicated: create an object which acts like a file object, and
        > which can collect blobs of data as you output them, but hold
        > them in memory, writing them all out together after you send
        > it the terminating sequence (\n in this case).[/color]

        A nice idea; maybe I will have a closer look at it.
        [color=blue]
        > A simpler option is just to collect up the bits of output that
        > you need in a list, then use the string join() method to generate
        > the output:
        >
        > outList = []
        > outList.extend(['abc', 'def'])
        > outList.append( 'ghi')
        > print ', '.join(outList)
        >[/color]

        I think this list approach is what I really meant.

        Thanks, also to Matt,
        Bertram

        --
        Bertram Scharpf
        Stuttgart, Deutschland/Germany

        Comment

        • Peter Otten

          #5
          Re: Separator in print statement

          Bertram Scharpf wrote:
          [color=blue]
          > when I write
          >[color=green][color=darkred]
          > >>> print 'abc', 'def',
          > >>> print 'ghi'[/color][/color]
          >
          > I get the output 'abc def ghi\n'.
          >
          > Is there a way to manipulate the print
          > statment that I get for example:
          >
          > 'abc, def, ghi\n'
          >
          > I mean: can I substitute the ' ' separator produced from
          > the comma operator by a e.g. ', ' or something else?[/color]

          Unfortunately, the delimiter for print is currently hardcoded.
          Here's some hackish code that will do what you want (most of the time).

          <code>
          import sys

          class DelimStream(obj ect):
          def __init__(self, stream, delim):
          self.stream = stream
          self.delim = delim
          self._softspace = False
          def _set_softspace( self, b):
          if b:
          self._softspace = True
          def _get_softspace( self):
          return False
          softspace = property(_get_s oftspace, _set_softspace)
          def write(self, s):
          if self._softspace :
          if s != "\n":
          self.stream.wri te(self.delim)
          self._softspace = False
          self.stream.wri te(s)

          d = DelimStream(sys .stdout, ", ")

          #works most of the time
          print >> d, "foolish", "consistenc y", "hobgoblin" , "little", "minds"
          print >> d, "seen", "hell", "himmel", "weich"

          #but not always:
          print "A", "B", "\n", "C"
          print >> d, "A", "B", "\n", "C" #note the missing delim before the C :-)


          if 1: #not recommended
          sys.stdout = DelimStream(sys .stdout, ", ")
          print "foolish", "consistenc y", "hobgoblin" , "little", "minds"
          </code>

          Peter

          Comment

          Working...