Re: Using print with format to stdout generates unwanted space



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 Watson wrote:
#!/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)


.



Relevant Pages

  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • Re: RfD: Escaped Strings
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... \b BS (backspace, ASCII 8) ... \ ** escapes to characters much as C does. ...
    (comp.lang.forth)
  • Re: A note on computing thugs and coding bums
    ... code is valid for any character set that is legal in C (which is a ... characters in the required source character set ... A String, in C Sharp or Java, can be redefined. ... allow programmers to handle some other data format, ...
    (comp.programming)

Loading