Print unicode string to console OK but fails when redirect to a file. How to fix?

后端 未结 4 816
离开以前
离开以前 2020-12-21 23:00

I have Python 2.7.1 on a Simplified-Chinese version of Windows XP, and I have a program like this(windows_prn_utf8.py):

#!/usr/bin/env python
# -*- coding: u         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-22 00:01

    You can encode it to utf-8 before you write it to file.

    f.write("电".encode("utf8"))
    

提交回复
热议问题