没有一个答案特别令人满意,因此这里有一个小片段可以帮助您。
class LineSeekableFile:
def __init__(self, seekable):
self.fin = seekable
self.line_map = list() # Map from line index -> file position.
self.line_map.append(0)
while seekable.readline():
self.line_map.append(seekable.tell())
def __getitem__(self, index):
# NOTE: This assumes that you're not reading the file sequentially.
# For that, just use 'for line in file'.
self.fin.seek(self.line_map[index])
return self.fin.readline()
用法示例:
In: !cat /tmp/test.txt
Out:
Line zero.
Line one!
Line three.
End of file, line four.
In:
with open("/tmp/test.txt", 'rt') as fin:
seeker = LineSeekableFile(fin)
print(seeker[1])
Out:
Line one!
这涉及到很多文件查找,但是对于无法将整个文件放入内存的情况很有用。它进行一次初始读取以获取行位置(因此它确实读取了整个文件,但并未将其全部保存在内存中),然后每次访问都根据事实查找文件。
根据用户的判断,我根据MIT或Apache许可提供了以上代码段。