Re: Using print with format to stdout generates unwanted space



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" <timh@xxxxxxxx> wrote in message
news:42b6bc99@xxxxxxxxxxxxxxxxxxxxxxx
> 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

  • Re: several simple questions about text boxes
    ... > I'm not the brightest MS Access user, so I need a little help. ... The answer depends on WHEN you need to get the length of the string. ... attempt to move off the control HAS NOT occurred. ... > Let's say I want to pull out the third character of any word, ...
    (microsoft.public.access.forms)
  • Re: Got UART Working!!! need syntax help with using ascii/buffer scheduling.
    ... it will use the character currently pointed to ... hardware and this is a description of the transmit holding register ... individual characters in the string is to use an index. ... characters, you can control that. ...
    (comp.arch.fpga)
  • Re: Replace a charater with a carriage return
    ... string together in the text box control source, ... section of the report the text box is in. ... ABC then a cube shaped character then EFG all on the same line. ...
    (microsoft.public.access.formscoding)
  • Re: Calculating the width of a Winforms control
    ... what character would you recommend I use to fill my "sizing" string. ... almost every font. ... So if my input is 6 characters, I could size my control ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: OpenVMS 5.5 shared folder on a windows network ?
    ... drivers do not give you control over these parameters (when the control is ...  The delay overhead of the USB isn't due the minimum "interrupt ... I understand the antique designs that required seeing the character "echo" ... The fact that parallel printer ports weren't designed to do simple ...
    (comp.os.vms)