我有一个非常大的4GB文件,当我试图读取它时,我的电脑挂了。 所以我想一块一块地读取它,在处理每一块之后,将处理过的一块存储到另一个文件中,然后读取下一块。
有什么方法可以生产这些碎片吗?
我喜欢有一个懒惰的方法。
我有一个非常大的4GB文件,当我试图读取它时,我的电脑挂了。 所以我想一块一块地读取它,在处理每一块之后,将处理过的一块存储到另一个文件中,然后读取下一块。
有什么方法可以生产这些碎片吗?
我喜欢有一个懒惰的方法。
当前回答
File.readlines()接受一个可选的size参数,它近似于在返回的行中读取的行数。
bigfile = open('bigfilename','r')
tmp_lines = bigfile.readlines(BUF_SIZE)
while tmp_lines:
process([line for line in tmp_lines])
tmp_lines = bigfile.readlines(BUF_SIZE)
其他回答
要编写惰性函数,只需使用yield:
def read_in_chunks(file_object, chunk_size=1024):
"""Lazy function (generator) to read a file piece by piece.
Default chunk size: 1k."""
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
with open('really_big_file.dat') as f:
for piece in read_in_chunks(f):
process_data(piece)
另一种选择是使用iter和helper函数:
f = open('really_big_file.dat')
def read1k():
return f.read(1024)
for piece in iter(read1k, ''):
process_data(piece)
如果文件是基于行的,那么文件对象已经是一个惰性的行生成器:
for line in open('really_big_file.dat'):
process_data(line)
我认为我们可以这样写:
def read_file(path, block_size=1024):
with open(path, 'rb') as f:
while True:
piece = f.read(block_size)
if piece:
yield piece
else:
return
for piece in read_file(path):
process_piece(piece)
由于我的低声誉,我不允许评论,但SilentGhosts解决方案应该更容易与file.readlines([sizehint])
Python文件方法
编辑:SilentGhost是对的,但这应该比:
s = ""
for i in xrange(100):
s += file.next()
f = ... # file-like object, i.e. supporting read(size) function and
# returning empty string '' when there is nothing to read
def chunked(file, chunk_size):
return iter(lambda: file.read(chunk_size), '')
for data in chunked(f, 65536):
# process the data
更新:该方法最好在https://stackoverflow.com/a/4566523/38592中解释
参考python的官方文档https://docs.python.org/3/library/functions.html#iter
也许这个方法更python化:
"""A file object returned by open() is a iterator with
read method which could specify current read's block size
"""
with open('mydata.db', 'r') as f_in:
block_read = partial(f_in.read, 1024 * 1024)
block_iterator = iter(block_read, '')
for index, block in enumerate(block_iterator, start=1):
block = process_block(block) # process your block data
with open(f'{index}.txt', 'w') as f_out:
f_out.write(block)