Pergunta

Existe alguma maneira para acessar o objeto de arquivo usado por um objeto CSV escritor / leitor depois de ter sido instanciado? Eu openned o módulo de csv, e parece contestar de são embutido. Eu também tentei definindo o objeto de arquivo como uma propriedade, mas eu recebo o seguinte erro:

AttributeError: '_csv.writer' object has no attribute 'fileobj'
Foi útil?

Solução

csv.writer is a "builtin" function. That is, it is written in compiled C code rather than Python. So its internal variables can't be accessed from Python code.

That being said, I'm not sure why you would need to inspect the csv.writer object to find out the file object. That object is specified when creating the object:

w = csv.writer(fileobj, dialect, ...)

So if you need to access that object later, just save it in another variable.

Outras dicas

From what I can tell, there is no straightforward way to get the file object back out once you put it into a csv object. My approach would probably be to subclass the csv writer and readers you're using so they can carry that data around with them. Of course, this assumes the ability to be able to directly access the types of classes the factory function makes (among other things).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top