通过继承`io.TextIOWrapper`对子文件进行子类化 - 但它的构造函数有哪些签名?

ger*_*rit 8 python io subclassing python-3.x

我试图子类io.TextIOWrapper下面这篇文章,虽然我的目标是不同的.从这开始(NB:动机):

class MyTextIOFile(io.TextIOWrapper):
    def read(self, *args):
        cont = super().read(*args)
        return cont.replace("\x00", "")
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用我的构造函数打开文件

In [81]: f = MyTextIOFile("file.csv")
Run Code Online (Sandbox Code Playgroud)

但这给了:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-90-343e18b2e32f> in <module>()
----> 1 f = MyTextIOFile("file.csv")

AttributeError: 'str' object has no attribute 'readable'
Run Code Online (Sandbox Code Playgroud)

事实上,似乎io.TextIOWrapper构造函数希望传递一个文件对象.通过反复试验,我发现这个文件对象需要以二进制模式打开.但是我无法在任何地方找到文档,而且我不想构建在无证件行为之上(实际上,尝试继续使用它已经导致我在尝试传递对象时遇到问题csv.reader).在Python 3中对文件对象进行子类化的正确和受支持的方法是什么?

我正在使用Python 3.5.0.

che*_*ner 2

我认为您正在寻找的文档是

class io.TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False)
    A buffered text stream over a BufferedIOBase binary stream. [...]
Run Code Online (Sandbox Code Playgroud)

第一个参数是二进制流,这意味着由 . 以二进制模式打开的内容open