python - 如何从 StringIO、BytesIO 等中删除字节

标签 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']

最佳答案

不,你不能,因为 BytesIO 是普通文件对象的内存版本。

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

您可能想查看 collections.deque() type相反。

关于python - 如何从 StringIO、BytesIO 等中删除字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590823/

相关文章:

python - 乘以归一化值 - 如何取单词的加权平均值

python - 我的 thrift ./configure 不构建 python 库

如果条件表达式的计算结果为 -1,Python if block 就会执行

python - 算术运算中的奇怪类型提升

python - Sublime Text键盘映射启动 "extension"

python - 导入模块不能通过终端工作,而通过 IDE 工作

python - types.MethodType 与 functools.partial

python - django.db.utils.InterfaceError : (0, '' )

Python Pandas 按多个条件过滤列

python - 在 hvplot 中自定义标记列表