在以下代码中,您能详细解释为什么 print s.encode('cp437')
失败,以及为什么会出现UnicodeDecodeError
错误?你能解释为什么 print x.decode('cp437')
失败了,为什么它是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