Problem with what raw_input() returns

Serge Orlov sombDELETE at pobox.ru
Tue Jan 13 08:45:06 CET 2004


"sebb" <sebb at linuxcult.com> wrote in message news:56f42e53.0401121746.39b9e4db at posting.google.com...
> If I do the following script:
>
> # -*- coding: cp1252 -*-
> one = "éé"
> two = one.replace("é","a")
>
> print two # it prints aa as I expected

It's not a good idea anyway, unless you know what you're doing.
Use unicode.

>
> But with the following script:
>
> # -*- coding: cp1252 -*-
> one = raw_input() # When I run the script, I type éé
> two = one.replace("é","a")
>
> print two # it still prints éé and I want it to print aa
>
>
> Can someone help me with that problem?
> Thanks

Use at least 2.3 Python. Then encode stdin like that:
import sys, codecs
sys.stdin = codecs.getreader(sys.stdout.encoding)(sys.stdin)
one = raw_input()
two = one.replace(u"é","a")

The code above assumes that Python was able to figure out
console encoding. If it's absent (sys.stdout.encoding == None)
you have to find it out yourself. Besides if it's absent you
need to encode stdout using codecs.getwriter as well.

-- Serge.





More information about the Python-list mailing list