Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当包含编写器()关闭StringIO时,我还可以使用它吗?_Python_File_Buffer_Avro - Fatal编程技术网

Python 当包含编写器()关闭StringIO时,我还可以使用它吗?

Python 当包含编写器()关闭StringIO时,我还可以使用它吗?,python,file,buffer,avro,Python,File,Buffer,Avro,我在用电脑。我想通过http发送一个avro文件,但我并不特别想先将该文件保存到磁盘,所以我想我应该使用StringIO来保存文件内容,直到我准备好发送为止。但是avro.datafile.DataFileWriter仔细地为我关闭了文件句柄,这使得我很难从StringIO中获取数据。以下是我在代码中的意思: from StringIO import StringIO from avro.datafile import DataFileWriter from avro import schema

我在用电脑。我想通过http发送一个avro文件,但我并不特别想先将该文件保存到磁盘,所以我想我应该使用StringIO来保存文件内容,直到我准备好发送为止。但是avro.datafile.DataFileWriter仔细地为我关闭了文件句柄,这使得我很难从StringIO中获取数据。以下是我在代码中的意思:

from StringIO import StringIO
from avro.datafile import DataFileWriter
from avro import schema, io
from testdata import BEARER, PUBLISHURL, SERVER, TESTDATA
from httplib2 import Http

HTTP = Http()
##
# Write the message data to a StringIO
#
# @return StringIO
#
def write_data():
    message = TESTDATA
    schema = getSchema()
    datum_writer = io.DatumWriter(schema)
    data = StringIO()
    with DataFileWriter(data, datum_writer, writers_schema=schema, codec='deflate') as datafile_writer:
        datafile_writer.append(message)
        # If I return data inside the with block, the DFW buffer isn't flushed
        # and I may get an incomplete file
    return data

##
# Make the POST and dump its response
#
def main():
    headers = {
        "Content-Type": "avro/binary",
        "Authorization": "Bearer %s" % BEARER,
        "X-XC-SCHEMA-VERSION": "1.0.0",
    }
    body = write_data().getvalue() # AttributeError: StringIO instance has no attribute 'buf'
    # the StringIO instance returned by write_data() is already closed. :(
    resp, content = HTTP.request(
        uri=PUBLISHURL,
        method='POST',
        body=body,
        headers=headers,
    )
    print resp, content
我确实有一些变通方法可以使用,但没有一个是非常优雅的。StringIO关闭后,有没有办法从中获取数据?

没有

文件对此非常清楚:

关闭

释放内存缓冲区。试图对关闭的StringIO对象执行进一步操作将引发ValueError

最干净的方法是从StringIO继承并重写close方法以不执行任何操作:

class MyStringIO(StringIO):
   def close(self):
       pass
   def _close(self):
       super(MyStringIO, self).close()
当你准备好的时候,叫你关门。

不太好

文件对此非常清楚:

关闭

释放内存缓冲区。试图对关闭的StringIO对象执行进一步操作将引发ValueError

最干净的方法是从StringIO继承并重写close方法以不执行任何操作:

class MyStringIO(StringIO):
   def close(self):
       pass
   def _close(self):
       super(MyStringIO, self).close()

准备好后调用_close。

我想做完全相同的事情,DataFileWriter有一个flush方法,因此您应该能够在调用后进行flush以追加数据,然后返回数据。对我来说,似乎比从StringIO派生类更优雅。

我想做完全相同的事情,DataFileWriter有一个flush方法,因此您应该能够在调用append之后进行flush,然后返回数据。对我来说,似乎比从StringIO派生类要优雅一点