gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:01:51 26 4
gpt4 key购买 nike

我想将 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com