Python 3, is using sys.stdout.buffer.write() good style?

前端 未结 2 1445
礼貌的吻别
礼貌的吻别 2021-02-08 19:49

After I learned about reading unicode files in Python 3.0 web script, now it\'s time for me to learn using print() with unicode.

I searched for writing unic

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-08 20:44

    This is an old answer but I'll add my version here since I first ventured here before finding my solution.

    One of the issues with codecs.getwriter is if you are running a script of sorts, the output will be buffered (whereas normally python stdout prints after every line).

    sys.stdout in the console is a IOTextWrapper, so my solution uses that. This also allows you to set line_buffering=True or False.

    For example, to set stdout to, instead of erroring, backslash encode all output:

    sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding=sys.stdout.encoding,
                                  errors="backslashreplace", line_buffering=True)
    

    To force a specific encoding (in this case utf8):

    sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding="utf8",
                                  line_buffering=True)
    

    A note, calling sys.stdout.detach() will close the underlying buffer. Some modules use sys.__stdout__, which is just an alias for sys.stdout, so you may want to set that as well

    sys.stdout = sys.__stdout__ = io.TextIOWrapper(sys.stdout.detach(), encoding=sys.stdout.encoding, errors="backslashreplace", line_buffering=True)
    sys.stderr = sys.__stderr__ = io.TextIOWrapper(sys.stderr.detach(), encoding=sys.stdout.encoding, errors="backslashreplace", line_buffering=True)
    

提交回复
热议问题