1

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
Juan T
  • 1,219
  • 1
  • 10
  • 21
user1187968
  • 7,154
  • 16
  • 81
  • 152
  • `s` is already encoded, and `x` is already decoded. You shouldn't try to re-encode/decode them. – user2357112 Feb 22 '17 at 22:20
  • why x is already decoded? – user1187968 Feb 22 '17 at 22:21
  • 1
    `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. – Bakuriu Feb 22 '17 at 22:26

0 Answers0