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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benjamin Rutt

    #1

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

    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.readl ine().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.Interactiv eConsole().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
  • Steve Holden

    #2
    Re: sys.stdin.readl ine() results in extra space send to stdout

    Benjamin Rutt wrote:[color=blue]
    > 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.readl ine().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.Interactiv eConsole().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,[/color]

    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.blogs pot.com

    Comment

    Working...