Separator in print statement

B

Bertram Scharpf

Hi,

when I write

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
 
M

Matt Goodall

Bertram said:
Hi,

when I write


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?

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.write().

Cheers, Matt
 
P

Peter Hansen

Bertram said:
when I write


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

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

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.
'abc, def, ghi\n'

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

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
 
B

Bertram Scharpf

Hi Peter,

thank you for your detailed answer.

Peter Hansen schrieb im Artikel said:
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.

Anyway. I mean ' ' when I say ' '.
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 nice idea; maybe I will have a closer look at it.
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 think this list approach is what I really meant.

Thanks, also to Matt,
Bertram
 
P

Peter Otten

Bertram said:
when I write


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?

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(object):
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_softspace, _set_softspace)
def write(self, s):
if self._softspace:
if s != "\n":
self.stream.write(self.delim)
self._softspace = False
self.stream.write(s)

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

#works most of the time
print >> d, "foolish", "consistency", "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", "consistency", "hobgoblin", "little", "minds"
</code>

Peter
 
B

Bengt Richter

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.

Maybe something familiar? ;-)

import sys
def printf(fmt, *args): sys.stdout.write(fmt%args)

or
def sprintf(fmt, *args): return fmt%args # ok, a little different ;-)

'abc, def, ghi\n'

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

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

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top