Using print with format to stdout generates unwanted space

P

Paul Watson

#!/usr/bin/env python

# Using a print statement to stdout results in an
# unwanted space character being generated at the
# end of each print output. Same results on
# DOS/Windows and AIX.
#
# I need precise control over the bytes that are
# produced. Why is print doing this?
#
import sys

# If this is a DOS/Windows platform, then put stdout
# into binary mode so that only the UNIX compatible newline
# will be generated.
#
try:
import msvcrt, os
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
except:
print 'This is not an msvcrt platform.'
pass

# Using print with newline suppressed generates a space at the
# end of each print statement.
#
for i in range(3):
print '%d,60,' % (i),
for j in range(10):
print '%d,' % (j),
print ''

# Using a list and doing a join does not result in the space
# character being generated.
#
for i in range(3):
alist = []
alist.append('%d,60,' % (i))
for j in range(10):
alist.append('%d,' % (j))
print ''.join(alist)

sys.exit(0)
 
J

John Roth

Don't use print, write directly to sys.stdout.
Print is not intended for precise output formatting;
it's intended for quick outputs that are useable
most of the time.

John Roth
 
T

Tim Hoffman

Hi Paul

Based on your description of what you want to do, print is probably not
the correct method of controlling output format. You should use write()
method of the file handle to get unadulterated output.

print is working as documented . From the Python 2.3 documentation,
Section 6.6 The Print statement.

"print evaluates each expression in turn and writes the resulting object
to standard output (see below). If an object is not a string, it is
first converted to a string using the rules for string conversions. The
(resulting or original) string is then written. A space is written
before each object is (converted and) written, unless the output system
believes it is positioned at the beginning of a line. This is the case
(1) when no characters have yet been written to standard output, (2)
when the last character written to standard output is "\n", or (3) when
the last write operation on standard output was not a print statement."

As you can see a space char is written and is correct as per the docs.

Rgds

Tim
 
P

Paul Watson

Thanks for all replies.

Ok. I agree. While printf() does tightly control formatting in C, it does
not in Python. Using write() can be used to output with no changes to the
data.

Tim Hoffman said:
Hi Paul

Based on your description of what you want to do, print is probably not
the correct method of controlling output format. You should use write()
method of the file handle to get unadulterated output.

print is working as documented . From the Python 2.3 documentation,
Section 6.6 The Print statement.

"print evaluates each expression in turn and writes the resulting object
to standard output (see below). If an object is not a string, it is first
converted to a string using the rules for string conversions. The
(resulting or original) string is then written. A space is written before
each object is (converted and) written, unless the output system believes
it is positioned at the beginning of a line. This is the case (1) when no
characters have yet been written to standard output, (2) when the last
character written to standard output is "\n", or (3) when the last write
operation on standard output was not a print statement."

As you can see a space char is written and is correct as per the docs.

Rgds

Tim

Paul said:
#!/usr/bin/env python

# Using a print statement to stdout results in an
# unwanted space character being generated at the
# end of each print output. Same results on
# DOS/Windows and AIX.
#
# I need precise control over the bytes that are
# produced. Why is print doing this?
#
import sys

# If this is a DOS/Windows platform, then put stdout
# into binary mode so that only the UNIX compatible newline
# will be generated.
#
try:
import msvcrt, os
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
except:
print 'This is not an msvcrt platform.'
pass

# Using print with newline suppressed generates a space at the
# end of each print statement.
#
for i in range(3):
print '%d,60,' % (i),
for j in range(10):
print '%d,' % (j),
print ''

# Using a list and doing a join does not result in the space
# character being generated.
#
for i in range(3):
alist = []
alist.append('%d,60,' % (i))
for j in range(10):
alist.append('%d,' % (j))
print ''.join(alist)

sys.exit(0)
 
M

Michael Hoffman

Paul said:
While printf() does tightly control formatting in C, it does not in
> Python.

There is no printf() in Python. You should not think of print as being a
Python version of printf.
 
T

Tim Williams (gmail)

There is no printf() in Python. You should not think of print as being a
Python version of printf.

For quick and simple removal of the extra space, append a '\b'
backspace character to your output "string"
 
J

Jorgen Grahn

For quick and simple removal of the extra space, append a '\b'
backspace character to your output "string"

For things that are only ever to be viewed on the terminal, yes.
But this does, of course, print an actual backspace character.
If you feed this output to another program, chances are it will
not treat <space><backspace> as no space at all.

I prefer building up a list and doing ' '.join(thelist) in these situations.

/Jorgen
 
M

Michael Hoffman

[Tim Williams]
[Jorgen Grahn]
For things that are only ever to be viewed on the terminal, yes.
But this does, of course, print an actual backspace character.
If you feed this output to another program, chances are it will
not treat <space><backspace> as no space at all.

I prefer building up a list and doing ' '.join(thelist) in these situations.

A much better approach. Or you can use sys.stdout.write() as others have
said.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top