[python] 파이썬에서 큰 파일을 읽는 게으른 방법?

나는 4GB의 매우 큰 파일을 가지고 있으며 그것을 읽으려고 할 때 컴퓨터가 정지합니다. 그래서 조각별로 읽고 각 조각을 처리 한 후 처리 된 조각을 다른 파일에 저장하고 다음 조각을 읽습니다.

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)


답변

컴퓨터, 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-ing 큰 파일은 주소 공간의 많은 부분을 예약하고 메모리 프로그램을 고갈시킬 수 있습니다.


답변

file.readlines() 리턴 된 행에서 읽은 행 수와 비슷한 선택적 크기 인수를 사용합니다.

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)


답변

이미 좋은 답변이 많이 있지만 전체 파일이 한 줄에 있고 여전히 고정 크기 블록이 아닌 “행”을 처리하려는 경우 이러한 답변이 도움이되지 않습니다.

시간의 99 %, 파일을 한 줄씩 처리 할 수 ​​있습니다. 그런 다음이 답변 에서 제안한 것처럼 파일 객체 자체를 지연 생성기로 사용할 수 있습니다.

with open('big.csv') as f:
    for line in f:
        process(line)

그러나, 나는 행 구분은 사실이 아니었다 매우 매우 큰 (거의) 한 줄 파일로 실행 된 한 번 '\n'하지만 '|'.

  • 한 줄씩 읽는 것은 옵션이 아니지만 여전히 한 줄씩 처리해야했습니다.
  • 이 csv의 일부 필드 (자유 텍스트 사용자 입력)가 포함되어 있기 때문에 처리 '|'하기 '\n'전에 변환 하는 것도 의문의 여지가 없습니다 '\n'.
  • csv 라이브러리를 사용하는 것도 최소한 초기 버전의 lib 에서 입력 행을 한 행씩 읽도록 하드 코딩되어 있기 때문에 배제되었습니다 .

이러한 종류의 상황을 위해 다음 스 니펫을 만들었습니다.

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(row)
    """
    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 : //.com/a/4566523/38592에서 가장 잘 설명됩니다.


답변

파이썬 공식 문서 https://docs.python.org/zh-cn/3/library/functions.html?#iter를 참조하십시오

어쩌면이 방법은 더 파이썬 일 수 있습니다.

from functools import partial

"""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:

    part_read = partial(f_in.read, 1024*1024)
    iterator = iter(part_read, b'')

    for index, block in enumerate(iterator, start=1):
        block = process_block(block)    # process block data
        with open(f'{index}.txt', 'w') as f_out:
            f_out.write(block)


답변

우리는 다음과 같이 쓸 수 있다고 생각합니다.

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)