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
>>>
2 Answers 2
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
Comments
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)
print(repr(s))LC_ALL=de_DE.cp437 python