[python]解决将 python 程序输出结果重定向到文件时,出现 UnicodeDecodeError 错误
比如:有一个 python 程序 hello.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print u'你好'
将输出结果重定向到文件,就会出现 UnicodeEncodeError:
$ python hello.py > hello.txt
Traceback (most recent call last):
File "hello.py", line 4, in <module>
print u'你好'
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not ...