I've found the solution. Just add one line in head of the python script:
# -*- coding: UTF-8 -*-
For example, a simple script named utf8.py:
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
s = u'中文'
print s.encode('utf-8')
Then redirect the print
to a txt file:
[zfz@server tmp]$ python utf8.py >> utf8.txt
[zfz@server tmp]$ cat utf8.txt
中文
The Chinese characters can be output to the txt file correctly.