pandas 使用chunkSize 读取大文件

时间:2019-06-26 21:21:22   收藏:0   阅读:1580

import pandas as pd
import numpy as np

import chardet

#检测文件编码
with open(r‘D:\test.txt‘, ‘rb‘) as fo:
  encode = chardet.detect(fo.readline())[‘encoding‘]
print(encode)

#建议如果检测出编码为ascii 则采用utf-8编码

reader = pd.read_csv(r‘D:\test.txt‘, iterator=True, encoding=encode)

loop = True
chunkSize = 10000# 每次读取的行数
while loop:
  try:
    chunk = reader.get_chunk(chunkSize)# type(chunk) is pd.DataFrame

    #需要注意的是文件的列名
    # do something 
  except StopIteration:
    loop = False
    print("Iteration is stopped.")

原文:https://www.cnblogs.com/linchee/p/11093367.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!