Strange behaviour of input() function (Python 3.2)

Chris Angelico rosuav at gmail.com
Mon May 23 18:42:14 EDT 2011


On Tue, May 24, 2011 at 1:44 AM, Aleksander Pietkiewicz
<sunrrrise at gmail.com> wrote:
> Hello,
> I have googled your email address, I hope it is not a problem.
> Thank you for your help!

I figured you would get it from my post, but either way works! My
email address is fairly well known. Sorry for the delay in response;
you caught me while I was asleep. :) I'm now responding on-list so
that other people can help.

> I agree that can be very specific bug, I suspect it is matter of coding. I'm
> emailing you a *.py file as you asked and screenshot showing script being
> run.

Unfortunately my Windows install doesn't have internationalization
support, which may be an issue here. I ran your 'couting.py' and got
errors back:
Traceback (most recent call last):
  File "foo.py", line 11, in <module>
    n=input("Naci\u015bnij Enter aby zako\u0144czy\u0107...")
  File "C:\python32\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character '\u015b' in position
4: character maps to <undefined>

So I'm guessing that codepage 437 is just plain wrong. But that
shouldn't affect your system.

> As you can see this problem occurs only with 3rd party software (like Komodo
> Edit).
> In, addition when I'm using Komodo or Notepad++ and input() function, Python
> miscount bytes. See attached.
> Once again thank you for your help!
> Kind regards,
> Aleksander Pietkiewicz

My suspicion here is that your editor is saving using one encoding,
and Python is expecting another. I recommend you put an encoding
marker at the top of your source file:

# coding=utf-8

See http://www.python.org/dev/peps/pep-0263/ for details. With this in
place, you should be able to guarantee that the bytestream is parsed
the same way by editor and interpreter.

Unfortunately that's all I could offer; I was unable to duplicate the
exact problem you were seeing. The contents of 'couting.py' are simple
enough, so I'll paste here in case anyone can spot a problem:


s = (input('Enter something : '))
z = input('Enter something : ')

print('Length of the string s is', len(s))
print('Length of the string z is', len(z))

print(s)
print(z)


Point to note: On my Windows XP, the string lengths are one higher
than expected, and they include a \r at the end. Is there any way that
this could trigger a Unicode parse failure??

Chris Angelico



More information about the Python-list mailing list