mysql导出数据写入文本转成excel文档

由于导出的数据量太大 预估可能需要百万行,用工具导几w行已是瓶颈,所以用sql查出数据 写入文本,再用Python写入excel。

select path ,size,createtime  
into outfile 'alldata.csv' FIELDS TERMINATED BY ','  OPTIONALLY ENCLOSED BY '"'  LINES TERMINATED BY '\n'   from pusher_2_days where createtime > '2016-01-18 13:00:00' and createtime<='2020-01-18 17:30:00'; 

把csv文档down到本机 写程序处理 需要先安装openpyxl包

import os
import openpyxl

def read_txt(filename,i):
	with open(filename,'r',encoding = 'utf-8') as f:
		for line in f.readlines():
			i=i+1
			write_excel(line,i)



def write_excel(line,row):
	#outwb = openpyxl.Workbook()
	#outws = outwb.create_sheet(index=0)

	#print(line.split(','))
	j = 0
	for col in line.split(','):
		replace_col = col.replace('"','')
		#print(col)
		j=j+1
		outws.cell(row=row, column = j).value = replace_col
		
		
		


if __name__ == '__main__':
	filelist = ['']
	#filelist = ['201911.csv',]
	csvfiledir = r'd:\work\2020'
	for fname in filelist:
		i=0
		csvfile = os.path.join(csvfiledir,fname)
		print(csvfile)
		outwb = openpyxl.Workbook()
		outws = outwb.create_sheet(index=0)
		read_txt(csvfile,i)
		savefile = os.path.splitext(fname)[0]+'.xlsx'
		print(savefile)
		saveexcel = os.path.join(csvfiledir,savefile) 
		outwb.save(saveexcel)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值