分块读取大文件方法

1 利用get_chunck()函数

loop为真时,get_chunck()函数会一直从文件中取数据,直到最后一次try不能执行了,执行except,loop置为假。

app_usage = pd.read_csv('D:\\HUAWEIcompetition\\data\\user_app_usage.csv', iterator=True)

pieceID=0
loop=True
while loop:
    try:
        dfi = app_usage.get_chunk(10000000)
        dfi.columns=['uid','appid','total_usagetime','open_numbers','use_date']
        dfi.to_csv('D:\\HUAWEIcompetition\\data\\user_app_usage\\user_app_usage_{}.csv'.format(pieceID),index=False)
        pieceID += 1
        del dfi
    except StopIteration:
        loop=False
        print('imps_log process finish!')

猜你喜欢

转载自www.cnblogs.com/xxswkl/p/10922403.html