4 GB の非常に大きなファイルがあり、それを読み取ろうとすると、コンピューターがハングします。だから私はそれを少しずつ読みたいと思っています。
これらの作品への方法はありますyield
か?
怠惰な方法があれば幸いです。
遅延関数を作成するには、次を使用します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
、ヘルパー関数を使用することです。
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)
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)
お使いのコンピューター、OS、および Python が 64 ビットの場合、mmap モジュールを使用してファイルの内容をメモリにマップし、インデックスとスライスでアクセスできます。ドキュメントの例を次に示します。
import mmap
with open("hello.txt", "r+") as f:
# memory-map the file, size 0 means whole file
map = mmap.mmap(f.fileno(), 0)
# read content via standard file methods
print map.readline() # prints "Hello Python!"
# read content via slice notation
print map[:5] # prints "Hello"
# update content using slice notation;
# note that new content must have same size
map[6:] = " world!\n"
# ... and read again using standard file methods
map.seek(0)
print map.readline() # prints "Hello world!"
# close the map
map.close()
コンピューター、OS、または python のいずれかが 32 ビットの場合、大きなファイルを mmap すると、アドレス空間の大部分が予約され、プログラムのメモリが不足する可能性があります。
すでに多くの良い答えがありますが、ファイル全体が 1 行にあり、(固定サイズのブロックではなく) 「行」を処理したい場合、これらの答えは役に立ちません。
99% の確率で、ファイルを 1 行ずつ処理できます。次に、この回答で提案されているように、ファイル オブジェクト自体を遅延ジェネレーターとして使用できます。
with open('big.csv') as f:
for line in f:
process(line)
ただし、行セパレーターがない非常に大きなファイルに遭遇する場合があります'\n'
(一般的なケースは です'|'
)。
'|'
することは、正当に含まれている可能性のあるフィールド(たとえば、フリー テキストのユーザー入力)'\n'
を台無しにする可能性があるため、オプションではない場合があります。'\n'
このような状況のために、次のスニペットを作成しました [Python 3.8+ 用に 2021 年 5 月に更新]:
def rows(f, chunksize=1024, sep='|'):
"""
Read a file where the row separator is '|' lazily.
Usage:
>>> with open('big.csv') as f:
>>> for r in rows(f):
>>> process(r)
"""
row = ''
while (chunk := f.read(chunksize)) != '': # End of file
while (i := chunk.find(sep)) != -1: # No separator found
yield row + chunk[:i]
chunk = chunk[i+1:]
row = ''
row += chunk
yield row
[古いバージョンの python の場合]:
def rows(f, chunksize=1024, sep='|'):
"""
Read a file where the row separator is '|' lazily.
Usage:
>>> with open('big.csv') as f:
>>> for r in rows(f):
>>> process(r)
"""
curr_row = ''
while True:
chunk = f.read(chunksize)
if chunk == '': # End of file
yield curr_row
break
while True:
i = chunk.find(sep)
if i == -1:
break
yield curr_row + chunk[:i]
curr_row = ''
chunk = chunk[i+1:]
curr_row += chunk
さまざまな問題を解決するためにうまく使用できました。さまざまなチャンク サイズで、広範囲にテストされています。自分自身を納得させる必要がある人のために、私が使用しているテストスイートを次に示します。
test_file = 'test_file'
def cleanup(func):
def wrapper(*args, **kwargs):
func(*args, **kwargs)
os.unlink(test_file)
return wrapper
@cleanup
def test_empty(chunksize=1024):
with open(test_file, 'w') as f:
f.write('')
with open(test_file) as f:
assert len(list(rows(f, chunksize=chunksize))) == 1
@cleanup
def test_1_char_2_rows(chunksize=1024):
with open(test_file, 'w') as f:
f.write('|')
with open(test_file) as f:
assert len(list(rows(f, chunksize=chunksize))) == 2
@cleanup
def test_1_char(chunksize=1024):
with open(test_file, 'w') as f:
f.write('a')
with open(test_file) as f:
assert len(list(rows(f, chunksize=chunksize))) == 1
@cleanup
def test_1025_chars_1_row(chunksize=1024):
with open(test_file, 'w') as f:
for i in range(1025):
f.write('a')
with open(test_file) as f:
assert len(list(rows(f, chunksize=chunksize))) == 1
@cleanup
def test_1024_chars_2_rows(chunksize=1024):
with open(test_file, 'w') as f:
for i in range(1023):
f.write('a')
f.write('|')
with open(test_file) as f:
assert len(list(rows(f, chunksize=chunksize))) == 2
@cleanup
def test_1025_chars_1026_rows(chunksize=1024):
with open(test_file, 'w') as f:
for i in range(1025):
f.write('|')
with open(test_file) as f:
assert len(list(rows(f, chunksize=chunksize))) == 1026
@cleanup
def test_2048_chars_2_rows(chunksize=1024):
with open(test_file, 'w') as f:
for i in range(1022):
f.write('a')
f.write('|')
f.write('a')
# -- end of 1st chunk --
for i in range(1024):
f.write('a')
# -- end of 2nd chunk
with open(test_file) as f:
assert len(list(rows(f, chunksize=chunksize))) == 2
@cleanup
def test_2049_chars_2_rows(chunksize=1024):
with open(test_file, 'w') as f:
for i in range(1022):
f.write('a')
f.write('|')
f.write('a')
# -- end of 1st chunk --
for i in range(1024):
f.write('a')
# -- end of 2nd chunk
f.write('a')
with open(test_file) as f:
assert len(list(rows(f, chunksize=chunksize))) == 2
if __name__ == '__main__':
for chunksize in [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]:
test_empty(chunksize)
test_1_char_2_rows(chunksize)
test_1_char(chunksize)
test_1025_chars_1_row(chunksize)
test_1024_chars_2_rows(chunksize)
test_1025_chars_1026_rows(chunksize)
test_2048_chars_2_rows(chunksize)
test_2049_chars_2_rows(chunksize)
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 で最もよく説明されています。
次のように書けると思います。
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]) を使用するとはるかに簡単になるはずです。
編集:SilentGhostは正しいですが、これは次よりも優れているはずです:
s = ""
for i in xrange(100):
s += file.next()
私も少し似たような状況にいます。チャンクサイズをバイト単位で知っているかどうかは明らかではありません。通常はしませんが、必要なレコード (行) の数はわかっています。
def get_line():
with open('4gb_file') as file:
for i in file:
yield i
lines_required = 100
gen = get_line()
chunk = [i for i, j in zip(gen, range(lines_required))]
更新:noskloに感謝します。これが私が意味したことです。チャンクの「間」の行が失われることを除いて、ほとんど機能します。
chunk = [next(gen) for i in range(lines_required)]
線を失うことなくトリックを行いますが、見栄えがよくありません。
行ごとに処理するには、これはエレガントなソリューションです。
def stream_lines(file_name):
file = open(file_name)
while True:
line = file.readline()
if not line:
file.close()
break
yield line
空行がない限り。