如何从StringIO,BytesIO等中删除字节

Dus*_*rea 1 python

我想将BytesIO对象用作连续缓冲区(常见用例)。但是,是否可以从头删除不再需要的字节?

好像不一样,因为只有一个truncate()方法。

['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', 'close', 'closed', 'detach', 'fileno', 'flush', 'getvalue', 'isatty', 'next', 'read', 'read1', 'readable', 'readinto', 'readline', 'readlines', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'writelines']
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

不,您不能像BytesIO普通文件对象的内存版本一样。

因此,它被视为可以覆盖或附加到的字节序列,就像文件从最前面删除元素效率不高一样,因为它需要完全重写后面的所有数据。

您可能想要查看collections.deque()类型