1

In the following Python 2.7 shell, I was expecting "s.decode('cp437')", and print(s) give me the same outputs, but they are not, what's reason here?

>>> import sys
>>> sys.stdout.encoding
'cp437'
>>> sys.stdin.encoding
'cp437'
>>>
>>> s = "Flügel"
>>> s
'Fl\x81gel'
>>> s.decode('cp437')
u'Fl\xfcgel'
>>>
>>> print(s)
Flügel
>>>
asked Feb 22, 2017 at 21:32
2
  • 1
    try print(repr(s)) Commented Feb 22, 2017 at 21:39
  • Most likely it's the terminal's encoding that is influencing the encoding here. Try mangling with locales on your system - maybe run LC_ALL=de_DE.cp437 python Commented Feb 22, 2017 at 21:48

2 Answers 2

1

This isn't caused by str.decode. When you enter an object in the interactive shell, it prints the representation of that object, not the object itself. For some types, like integers, these display the same thing:

>>> i = 42
>>> i
42
>>> print(repr(i))
42
>>> print(i)
42

For other types, like strings, they display two different things:

>>> s = 'Flügel'
>>> s
'Fl\x81gel'
>>> print(repr(s))
'Fl\x81gel'
>>> print(s)
Flügel

Similarly with a Unicode object:

>>> u = s.decode('cp437')
>>> u
u'Fl\xfcgel'
>>> print(repr(u))
u'Fl\xfcgel'
>>> print(u)
Flügel

It may be helpful (or confusing) to note that in the case of strings and Unicode objects, the object's representation is the Python code to instantiate it. This may be clearer with a demonstration using datetime.datetime, which adopts the same paradigm:

>>> d = datetime.datetime(2012, 12, 20, 23, 59, 59)
>>> d
datetime.datetime(2012, 12, 20, 23, 59, 59)
>>> print(repr(d))
datetime.datetime(2012, 12, 20, 23, 59, 59)
>>> print(d)
2012年12月20日 23:59:59
answered Feb 22, 2017 at 22:43
Sign up to request clarification or add additional context in comments.

Comments

1

decode will return the decoded value, not replace s.

If you want to alter your variable,

s = s.decode('cp437')

(Interesting that the documentation simply says it decodes the string, while the encode documentation states it returns an encoded string)

answered Feb 22, 2017 at 21:36

2 Comments

This is irrelevant - at least for the s and s.decode('cp437') commands - their output differ
The question is why print(s) and decode give different results.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.