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