Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > sys.stdin.readline() results in extra space send to stdout

Reply
Thread Tools

sys.stdin.readline() results in extra space send to stdout

 
 
Benjamin Rutt
Guest
Posts: n/a
 
      03-06-2006
There has been a problem that has been bugging me for a while for
reading input from standard in. Consider the following simple program:

#!/usr/bin/env python
import sys
print 'enter something: ',
answer = sys.stdin.readline().strip()
print 'you answered {%s}' % (answer)

When I run this interactively, the following happens:

$ ~/tmp/foo.py
enter something: hi
you answered {hi}

Notice the extra space before 'you'; I did not put it there. It seems
that this problem can be avoided if I instead use the program:

#!/usr/bin/env python
import code
answer = code.InteractiveConsole().raw_input('enter something: ')
print 'you answered {%s}' % (answer)

Now, the output is:

$ ~/tmp/foo.py
enter something: hi
you answered {hi}

Is this a well-known problem? Is it a bug? I do not see why that
extra space is getting there in the first version. Using the code
module seems a little dirty, when sys.stdin is available. This is
python 2.4 on a Linux platform. Thank you,
--
Benjamin Rutt
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      03-06-2006
Benjamin Rutt wrote:
> There has been a problem that has been bugging me for a while for
> reading input from standard in. Consider the following simple program:
>
> #!/usr/bin/env python
> import sys
> print 'enter something: ',
> answer = sys.stdin.readline().strip()
> print 'you answered {%s}' % (answer)
>
> When I run this interactively, the following happens:
>
> $ ~/tmp/foo.py
> enter something: hi
> you answered {hi}
>
> Notice the extra space before 'you'; I did not put it there. It seems
> that this problem can be avoided if I instead use the program:
>
> #!/usr/bin/env python
> import code
> answer = code.InteractiveConsole().raw_input('enter something: ')
> print 'you answered {%s}' % (answer)
>
> Now, the output is:
>
> $ ~/tmp/foo.py
> enter something: hi
> you answered {hi}
>
> Is this a well-known problem? Is it a bug? I do not see why that
> extra space is getting there in the first version. Using the code
> module seems a little dirty, when sys.stdin is available. This is
> python 2.4 on a Linux platform. Thank you,


It's related to the mechanism for printing. Since the last print
statement executed had a trailing comma, the next one starts its output
with a sapce. The interpreter doesn't realise that the "carriage" has
been "returned" by the input.

raw_input() is the bext way to go.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
where the extra space comes from on the stdout alf Python 4 12-09-2006 05:04 AM
Help w/extra space between controls (and i don't want the space) =?Utf-8?B?ZHc=?= ASP .Net 5 07-30-2006 04:32 PM
Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok?? Christian Seberino Python 21 10-27-2003 04:20 PM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-23-2003 07:07 AM
Stack space, global space, heap space Shuo Xiang C Programming 10 07-11-2003 07:30 PM



Advertisments