【发布时间】:2013-11-15 18:33:32
【问题描述】:
我想了解在不将整个文件的内容加载到内存中的情况下,反向读取 gzip 文件的最有效(速度和性能)方法是什么。
这是我目前所做的,但对于非常大的文件效率不高:
file = 'huge_file.log.gz'
import gzip
if file.endswith('gz'):
f = gzip.open(file)
# reverse the file contents
reverse_file_list = reversed(f.read().decode('utf-8').split('\n'))
我看到在 stackoverflow 和 codestate 中有一些解决方案可以进行负搜索,但是当文件以二进制模式打开时不支持负搜索,例如 gzip.open
链接: Most efficient way to search the last x lines of a file in python
http://code.activestate.com/recipes/439045/
因此,我想要完成的解决方案失败了。
【问题讨论】: