在如下设置中写入文件时,production.iterlines()
会生成许多行文本:
with open('output.txt', 'w', encoding='UTF-8') as f:
for line in production.iterlines():
f.write(line)
然后-如果我理解正确-会发生以下情况:
TextIOWrapper对象f
逐行接受并对其进行编码,并且,每当达到某个限制(例如缓冲区中的4096个字节)时,就会将一个字节块写入磁盘。因此,实际的磁盘写入操作与生成的行的速度不同,但通常不太频繁(除非行很长)。在
现在我想在这些块上有一个迭代器,例如通过网络发送它们。它可以这样使用:
^{pr2}$因此,这个假设的buffereditor对象将参数中的字符串组合成大小合理的块,并将它们作为编码字节提供给调用方。在
我觉得通过巧妙地组合io
模块中的类,这应该是可能的——我只是不知道如何使用它们。类似于StringIO,但具有某种队列行为,具有交错的写入和读取调用。在
但也许我错了,他们不是为这种任务而设计的。(毕竟,在达到固定的块大小之前,拼凑一个对字符串进行编码和连接的迭代器并不是那么难的事情。)我们鼓励您给出答案,让我更好地理解io
类的用法。我通常非常喜欢官方文档,但是io
模块上的文档却让我失望地落在后面。在
这里不需要使用任何类。实际上,您并不是在执行I/O,而是在进行缓冲(这不是I/O所独有的)。尝试重用}可用于填充缓冲区。在
BufferIOBase
接口在这里是次优的,因为您的输入是iterable,输出也是iterable;两者都不是类似于文件的对象,.read()
也没有{您可以自己实现该类;我使用了^{} type 使其尽可能高效:
通过使用两个
deque
对象,一个用于长度,一个用于行本身,我们避免了过于频繁地调用len()
。获取一个字符串的长度是足够便宜的,但是每次推送和弹出当前堆栈帧来检索它的成本很高,因此需要避免这种情况。在演示:
^{pr2}$相关问题 更多 >
编程相关推荐