0

There are UnicodeDecodeError even after I set the errors attribute of StreamWriter to ignore. Here is the code:

import codecs
import platform
if platform.system() == 'Linux':
    writer = codecs.getwriter('utf8')
    sys.stdout = writer(sys.stdout) # for writing to pipes
elif platform.system() == 'Windows':
    writer = codecs.getwriter('gbk')
    sys.stdout = writer(sys.stdout, 'ignore') # for writing to pipes

I run the script and get UnicodeDecodeError when print u'长城'.

Can anybody fix this for me ?

Update: the full errors is:

Traceback (most recent call last):
  File ".\main.py", line 42, in <module>
    print u'闀垮煄'
  File "D:\Program files\Python2.7.6\lib\codecs.py", line 351, in write
    data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb3 in position 0: ordinal
not in range(128)

My OS is Windows 7.

Shuai Zhang
  • 2,011
  • 3
  • 22
  • 23

0 Answers0