Problem with what raw_input() returns

S

sebb

If I do the following script:

# -*- coding: cp1252 -*-
one = "éé"
two = one.replace("é","a")

print two # it prints aa as I expected


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
 
T

Terry Reedy

sebb said:
# -*- coding: cp1252 -*-
one = "éé"
two = one.replace("é","a")

print two # it prints aa as I expected

# -*- 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

Learn some basic debugging with print, repr, and ==; or if you know it, use
it; or if you did, share results with us ;-)

In particular, use different name for second input, print both before
replace attempt, and use explicit equality tests.
oneb = raw_input()
print repr(one), repr(oneb), one == oneb

Then draw conclusions and possibly make further test according to results.

Terry J. Reedy
 
S

Serge Orlov

sebb said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top