问题描述
假设我有一些StringIO
(来自cStringIO
).我想从中读取缓冲区,直到遇到某个字符/字节,例如"Z",所以:
Suppose I have some StringIO
(from cStringIO
). I want to read buffer from it until some character/byte is encountered, say 'Z', so:
stringio = StringIO('ABCZ123')
buf = read_until(stringio, 'Z') # buf is now 'ABCZ'
# strinio.tell() is now 4, pointing after 'Z'
在Python中最快的方法是什么?谢谢
What is fastest way to do this in Python? Thank you
推荐答案
我非常失望这个问题在堆栈溢出时仅得到一个答案,因为这是一个有趣且相关的问题.无论如何,由于只有ovgolovin给出了解决方案,并且我认为它可能很慢,所以我认为可以采用更快的解决方案:
I very disappointed that this question get only one answer on stack overflow, because it is interesting and relevant question. Anyway, since only ovgolovin give solution and I thinked it is maybe slow, I thought a faster solution:
def foo(stringio):
datalist = []
while True:
chunk = stringio.read(256)
i = chunk.find('Z')
if i == -1:
datalist.append(chunk)
else:
datalist.append(chunk[:i+1])
break
if len(chunk) < 256:
break
return ''.join(datalist)
以块为单位读取io(也许未在第一个块中找到结束字符).这是非常快的,因为没有为每个字符调用任何Python函数,而是相反,最大程度地使用了C编写的Python函数.
This read io in chunks (maybe end char found not in first chunk). It is very fast because no Python function called for each character, but on the contrary maximal usage of C-written Python functions.
运行速度比ovgolovin的解决方案快60倍.我运行了timeit
进行检查.
This run about 60x faster than ovgolovin's solution. I ran timeit
to check it.
这篇关于从StringIO读取直到遇到某个字节的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!