我想将 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']
最佳答案
不,你不能,因为 BytesIO
是普通文件对象的内存版本。
因此,它被视为可以覆盖或追加的字节序列,就像文件一样,从前面删除元素效率不高,因为它需要完全重写后面的所有数据。
您可能想查看 collections.deque()
type相反。
关于python - 如何从 StringIO、BytesIO 等中删除字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590823/