python分块读取大数据,避免内存不足的方法

import pandas as pd
def read_data(file_name):
    '''
    file_name:文件地址
    '''
    inputfile = open(file_name, 'rb') 
    data = pd.read_csv(inputfile, iterator=True)
    loop = True
    chunkSize = 1000 
    chunks = []
    while loop:
        try:
            chunk = dcs.get_chunk(chunkSize)
            chunks.append(chunk)
        except StopIteration:
            loop = False
            print("Iteration is stopped.")
    data = pd.concat(chunks, ignore_index=True)
    
    return data

你可能感兴趣的:(python面试题)