Stack Exchange
Why Python (2.7) encode and decode functions failed
In the following code, can you explain in detail why print s.encode('cp437') failed, and why it's an UnicodeDecodeError error? Can you also explain why print x.decode('cp437') failed, and why it's UnicodeEncodeError? 
# -*- coding: cp437 -*-
import sys

print sys.stdout.encoding       # cp437
print sys.stdin.encoding        # cp437
print ""

s = "Flügel"
print(s)                        # Flügel
#print s.encode('cp437')        # UnicodeDecodeError: 'ascii' codec can't decode byte 0x81
                                #   in position 2: ordinal not in range(128)
print s.decode('cp437')         # Flügel

x = u"Flügel"
print(x)                        # Flügel
#print x.decode('cp437')        # UnicodeEncodeError: 'ascii' codec can't encode character
                                #   u'\xfc' in position 2: ordinal not in range(128)
print x.encode('cp437')         # Flügel


s is already encoded, and x is already decoded. You shouldn't try to re-encode/decode them.

why x is already decoded?

encode is a method that converts a unicode to a bytes object. It takes a sequence of unicode codepoints and transforms it into a sequence of bytes that you can store in a file (by using a certain encoding). decode is an operation that you can apply to a bytes object to convert it into a unicode. So it does not make sense to call decode on a unicode object, nor encode on a bytes object.
