当导出的数据量太大,如果直接用 pandas 的 to_excel 或者 to_csv 去保存,会报以下错误:
data.to_csv('xxxx.csv')
ValueError: This sheet is too large! Your sheet size is: 16388602, 3 Max sheet size is: 1048576, 16384
解决办法:
方法其实很简单,使用 to_csv 模块是将文件的后缀名改为 .txt 并改用特殊分隔符例如 \t 即可。代码如下:
data.to_csv('xxxx.txt', sep='\t')
import pandas as pd
from datetime import datetime
ioutput = r'F:\\02-data'
with pd.ExcelWriter(output + f"/{datetime.now().strftime('%m%d_%H%M')}.xlsx") as writer:
df1.to_excel(writer, sheet_name='sheet_name1')
df2.to_excel(writer, sheet_name='sheet_name2')
writer.save()
writer.close()
for t_itvl in ['10min', '1hour']:
df1 = func(t_itvl)
df2 = func2(df1)
output_path = pd.ExcelWriter(
r'E:\02-data\output\\' + '' + t_itvl + '_eog{}.xlsx'.format(
datetime.now().strftime('%m%d_%H%M')))
df1.to_excel(output_path, sheet_name='ptp_' + str(t_itvl), encoding='gbk')
df2.to_excel(output_path, sheet_name='ptp_nor_' + str(t_itvl), encoding='gbk')
print('finish calculate {} -{}'.format(t_itvl, datetime.now().strftime('%m%d_%H%M')))
output_path.save()
output_path.close()