python面试题十(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
发布了44 篇原创文章 · 获赞 0 · 访问量 1226

猜你喜欢

转载自blog.csdn.net/weixin520520/article/details/105451313