如何在 Python 的 tempfile.TemporaryFile() 中指定文本模式?

How to specify text mode in Python's tempfile.TemporaryFile()?

documentation 中,TemporaryFile() 提到

The returned object is a file-like object whose _file attribute is either an io.BytesIO or io.StringIO object (depending on whether binary or text mode was specified)...

但是,从 Python 3.6 开始,没有 text=True 请求文本模式的方法。怎么做到的?

临时文件签名:

 tempfile.TemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None)

我们对 mode 参数感兴趣。最后一个符号是'b',表示二进制模式。如果你传递mode='wt',它将以文本模式打开。

本页描述了所有模式:https://docs.python.org/3/library/functions.html#open