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

Discussion in 'Python' started by Benjamin Rutt, Mar 6, 2006.

  1. 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
     
    Benjamin Rutt, Mar 6, 2006
    #1
    1. Advertising

  2. Benjamin Rutt

    Steve Holden Guest

    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
     
    Steve Holden, Mar 6, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Maxwell

    sys.stdin.readline()

    Mike Maxwell, Aug 31, 2004, in forum: Python
    Replies:
    12
    Views:
    1,621
    Mike Maxwell
    Sep 3, 2004
  2. Christian Convey

    Delays getting data on sys.stdin.readline() ?

    Christian Convey, Nov 20, 2005, in forum: Python
    Replies:
    3
    Views:
    547
    Fredrik Lundh
    Nov 20, 2005
  3. Replies:
    2
    Views:
    689
    velle
    Jan 5, 2006
  4. ywzhan
    Replies:
    2
    Views:
    543
    Dennis Lee Bieber
    Jun 11, 2006
  5. Replies:
    0
    Views:
    507
Loading...

Share This Page