利用pandas将mysql查询出得结果写入到excel文件

#!/usr/bin/env python3

import pandas as pd

import pymysql

#返回SQL结果的函数

def getrel(sql):

conn =  pymysql.connect(host='localhost',user='root',password='123456',db='test')

cur = conn.cursor()

cur.execute('set names utf8')

cur.execute('select app,name  from tb')  # 输入要查询的SQL

rel= cur.fetchall()

cur.close()

conn.close()

return rel

#生成xlsx文件的函数

def getxlsx(rel,dt):

dret = pd.DataFrame.from_records(list(rel))  # mysql查询的结果为元组,需要转换为列表

dret.to_excel("filename.xlsx",index=False,header=("app","name"))#header 指定列名,index 默认为True,写行名

## xlsx文件默认使用xlsxwriter,可以通过engine="xlsxwriter"指定

原文地址:http://blog.51cto.com/frees/2123535

时间: 2024-08-29 22:37:34

利用pandas将mysql查询出得结果写入到excel文件的相关文章

在ASP.NET MVC中利用Aspose.cells 将查询出的数据导出为excel,并在浏览器中下载。

正题前的唠叨 本人是才出来工作不久的小白菜一颗,技术很一般,总是会有遇到一些很简单的问题却不知道怎么做,这些问题可能是之前解决过的.发现这个问题,想着提升一下自己的技术水平,将一些学的新的'好'东西记录下来,一是加深印象:二是以后可以作为参考:三是希望博友们可以提出不足和可以优化的地方,一起讨论. 这个是我去一家公司没多久,让我做的小功能,主要是导出excel并在浏览器下载下来. 但是会有不同的细微的需求差别. 第一次发博客,有描述不清楚的地方还请见谅,希望各位多多指点. 进入正题 简单的需求描

pandas把读取sql的结果写入到excel文件

1.利用pandas模块 # encoding: utf-8 import time import pandas as pd import pymysql def getrel(sql): ''' 连接mysql数据库,根据条件查询出来我们所需要数据 :return: 根据条件从sql查询出来的数据 ''' conn = pymysql.connect(host='localhost', user='root', password='123', db='db_test', charset='ut

mysql查询出所有重复的记录

假如我们有如下一张数据表(很简单,只是举例而已),表名为student.现在我们要取出其中重复记录.重复是以name相同为判定标准. ID name phone age 1 张三 10086 15 2 李四 10010 20 3 王五 10010 22 4 赵六 10010 22 5 孙七 10010 22 查找出所有的重复数据 SELECT * FROM student WHERE phone IN (SELECT shortname FROM student GROUP BY phone H

Mysql查询出所有列名

select group_concat(COLUMN_NAME Separator ',') as COLUMN_NAME from information_schema.COLUMNS where table_name = 't_tk_question_info' and table_schema = 'dsideal_db' ; select group_concat(COLUMN_NAME Separator ',') as COLUMN_NAME from information_sch

利用spring batch 读取数据库中的数据写入到txt文件中

写一个job,其配置文件为: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springfram

mysql查询练习

mysql> #查询每个栏目最贵的商品 mysql> select goods_id,shop_price,cat_id from (select goods_id,shop_price,cat_id from goods order by shop_price desc) as temp group by cat_id; +----------+------------+--------+ | goods_id | shop_price | cat_id | +----------+----

mysql查询语句集

1. mysql 查询出某字段的值不为空的语句 1.不为空 select * from table where id <> ""; select * from table where id != ""; 2.为空 select * from table where id =""; select * from table where isNull(id); 具体情况具体分析,如果字段是char或者varchar类型的,使用id=&quo

mysql查询结果输出到文件

mysql查询结果导出/输出/写入到文件 方法一:直接执行命令:mysql> select count(1) from table  into outfile '/tmp/test.xls'; Query OK, 31 rows affected (0.00 sec)在目录/tmp/下会产生文件test.xls遇到的问题:mysql> select count(1) from table   into outfile '/data/test.xls';报错:ERROR 1 (HY000): C

python实现自动从mysql数据库取指定数据记录到excel中-新建、追加

xlsxwriter,openpyxl,pandas 模块都可以实现往excel中写入数据,但是为了更简单方便的实现我的需求,选择将三种结合使用. #!/usr/bin/env python3 # -*-coding: utf-8 -*- # @Time:2019/12/26 16:55 # @Author: WSN import pandas as pd import pymysql, openpyxl, os, xlsxwriter # 设定excel文件名称 version = 'V1.4