python3 使用openpyxl将mysql数据写入xlsx的操作


Posted in Python onMay 15, 2020

编程的生活愈发不容易了,工作越来越难找,说多了都是泪还是给大家贡献些代码比较实际。

python3 链接数据库需要下载名为pymysql的第三方库

python3 读写xlsx需要下载名为openpyxl的第三方库

在此我只贡献链接数据库和写入xlsx的代码

import pymysql.cursors
from fj.util import logger
from openpyxl import Workbook
from openpyxl.compat import range
from openpyxl.utils import get_column_letter
 
# 链接数据库的游标
connect = pymysql.Connect(
  host="localhost",
  port=3306,
  user='root',
  passwd='123456',
  db='zyDB',
  charset='utf8',
)
cursor = connect.cursor()
 
# 关闭数据库链接操作
def clos_cursor():
  return cursor.close();
 
# 读取数据库数据
def query_all():
  select_sql = "select*from fj_date where fj_id not in" \
         "( select a.fj_id from ( select * from fj_date where mj_id>0 ) a " \
         "join ( SELECT * from fj_date where jb_id>0 ) b" \
         " on a.fjzz = b.fjzz and a.fj_add=b.fj_add) and mj_id>0"
  cursor.execute(select_sql);
  return cursor.fetchall();
 
# 关闭数据库链接操作
def clos_cursor():
   cursor.close();
   connect.close()
 
def read_mysql_to_xlsx():
 
  #要创建的xlsx名称
  dest_filename = 'jb_data.xlsx'
 
  wb = Workbook()
  ws1 = wb.active
  ws1.title = "fj_date"
  # 列名
  ws1.cell(row=1,column=1,value="fj_id(数据库编号)")
  ws1.cell(row=1,column=2,value="jb_id(疾病编号)")
  ws1.cell(row=1,column=3,value="mj_id(名医编号)")
  ws1.cell(row=1,column=4,value="fj_name(方剂名称)")
  ws1.cell(row=1,column=5,value="fjcc(出处)")
  ws1.cell(row=1,column=6,value="fjdm(代码)")
  ws1.cell(row=1,column=7,value="fjzc(加减)")
  ws1.cell(row=1,column=8,value="fjgx(功效)")
  ws1.cell(row=1,column=9,value="fj_add(组成)")
  ws1.cell(row=1,column=10,value="fjjj(禁忌)")
  ws1.cell(row=1,column=11,value="fjzy(方剂治验)")
  ws1.cell(row=1,column=12,value="fjzz(主治)")
  ws1.cell(row=1,column=13,value="fjyf(用法)")
  ws1.cell(row=1,column=14,value="ylzy(药理作用)")
  ws1.cell(row=1,column=15,value="gjls(各家论述)")
  ws1.cell(row=1,column=16,value="fj(方解)")
  ws1.cell(row=1,column=17,value="ks(科室)")
  ws1.cell(row=1,column=18,value="ckzl(参考资料)")
  ws1.cell(row=1,column=19,value="lcyy(临床应用)")
  ws1.cell(row=1,column=20,value="tjbq(推荐标签)")
  ws1.cell(row=1,column=21,value="zysx(注意事项)")
  ws1.cell(row=1,column=22,value="fjzb(制备方法)")
  ws1.cell(row=1,column=23,value="fg(方歌)")
  ws1.cell(row=1,column=24,value="path(路径)")
  # 循环数据写入内容
  jb_date_list = query_all()
  for i in range(2,len(jb_date_list)+1):
    ws1.cell(row=i, column=1, value=jb_date_list[i-1][0])
    ws1.cell(row=i, column=2, value=jb_date_list[i-1][1])
    ws1.cell(row=i, column=3, value=jb_date_list[i-1][2])
    ws1.cell(row=i, column=4, value=jb_date_list[i-1][3])
    ws1.cell(row=i, column=5, value=jb_date_list[i-1][4])
    ws1.cell(row=i, column=6, value=jb_date_list[i-1][5])
    ws1.cell(row=i, column=7, value=jb_date_list[i-1][6])
    ws1.cell(row=i, column=8, value=jb_date_list[i-1][7])
    ws1.cell(row=i, column=9, value=jb_date_list[i-1][8])
    ws1.cell(row=i, column=10, value=jb_date_list[i-1][9])
    ws1.cell(row=i, column=11, value=jb_date_list[i-1][10])
    ws1.cell(row=i, column=12, value=jb_date_list[i-1][11])
    ws1.cell(row=i, column=13, value=jb_date_list[i-1][12])
    ws1.cell(row=i, column=14, value=jb_date_list[i-1][13])
    ws1.cell(row=i, column=15, value=jb_date_list[i-1][14])
    ws1.cell(row=i, column=16, value=jb_date_list[i-1][15])
    ws1.cell(row=i, column=17, value=jb_date_list[i-1][16])
    ws1.cell(row=i, column=18, value=jb_date_list[i-1][17])
    ws1.cell(row=i, column=19, value=jb_date_list[i-1][18])
    ws1.cell(row=i, column=20, value=jb_date_list[i-1][19])
    ws1.cell(row=i, column=21, value=jb_date_list[i-1][20])
    ws1.cell(row=i, column=22, value=jb_date_list[i-1][21])
    ws1.cell(row=i, column=23, value=jb_date_list[i-1][22])
    ws1.cell(row=i, column=24, value=jb_date_list[i-1][23])
 
  # 创建xlsx
  wb.save(filename=dest_filename)
 
if __name__ == '__main__':
  read_mysql_to_xlsx()

补充知识:Python 关闭文件释放内存的疑惑

我用with语句打开了一个4g的文件读取内容,然后程序末尾设置一个死循环,按理说with语句不是应该自动关闭文件释放资源吗?

但是系统内存一直没有释放。应该是被文件读取到的变量content一直占用吗?把content删除就会释放内存。或者去掉死循环,程序退出资源就自动释放了

既然这样的话关闭文件貌似没啥作用呢?具体释放了什么资源?

Python一直占用着将近5G的内存:

python3 使用openpyxl将mysql数据写入xlsx的操作

官方文档:

If you're not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it. If you don't explicitly close a file, Python's garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times.

After a file object is closed, either by a with statement or by calling f.close(), attempts to use the file object will automatically fail.

代码如下:

import sys
with open(r'H:\BaiduNetdiskDownload\4K.mp4','rb') as f:
  print(f.closed)
  content=f.read()
print(f.closed)
print(sys.getrefcount(f))
while True:
  pass

以上这篇python3 使用openpyxl将mysql数据写入xlsx的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python通过urllib2爬网页上种子下载示例
Feb 24 Python
web.py获取上传文件名的正确方法
Aug 26 Python
谈谈如何手动释放Python的内存
Dec 17 Python
Python3利用SMTP协议发送E-mail电子邮件的方法
Sep 30 Python
Python解决N阶台阶走法问题的方法分析
Dec 28 Python
Python subprocess模块详细解读
Jan 29 Python
python看某个模块的版本方法
Oct 16 Python
python write无法写入文件的解决方法
Jan 23 Python
python pygame实现方向键控制小球
May 17 Python
详解pycharm自动import所需的库的操作方法
Nov 30 Python
python函数超时自动退出的实操方法
Dec 28 Python
Pytorch 中net.train 和 net.eval的使用说明
May 22 Python
使用Python实现将多表分批次从数据库导出到Excel
May 15 #Python
解决python执行较大excel文件openpyxl慢问题
May 15 #Python
python可迭代对象去重实例
May 15 #Python
python 操作mysql数据中fetchone()和fetchall()方式
May 15 #Python
Python实现UDP程序通信过程图解
May 15 #Python
解决pymysql cursor.fetchall() 获取不到数据的问题
May 15 #Python
python如何解析复杂sql,实现数据库和表的提取的实例剖析
May 15 #Python
You might like
php数组函数序列之array_unshift() 在数组开头插入一个或多个元素
2011/11/07 PHP
对象失去焦点时自己动提交数据的实现代码
2012/11/06 PHP
php生成静态页面的简单示例
2014/04/17 PHP
PHP图像处理类库及演示分享
2015/05/17 PHP
PHP生成随机数的方法总结
2018/03/01 PHP
laravel使用Faker数据填充的实现方法
2019/04/12 PHP
浅析PHP反序列化中过滤函数使用不当导致的对象注入问题
2020/02/15 PHP
C#中TrimStart,TrimEnd,Trim在javascript上的实现
2011/01/17 Javascript
JavaScript打字小游戏代码
2011/12/26 Javascript
JS操作Cookies包括(读取添加与删除)
2012/12/26 Javascript
JavaScript实现单击下拉框选择直接跳转页面的方法
2015/07/02 Javascript
基于JavaScript实现全屏透明遮罩div层锁屏效果
2016/01/26 Javascript
JS/jQ实现免费获取手机验证码倒计时效果
2016/06/13 Javascript
jQuery获取table行数并输出单元格内容的实现方法
2016/06/30 Javascript
利用Angularjs和原生JS分别实现动态效果的输入框
2016/09/01 Javascript
微信小程序 网络请求(GET请求)详解
2016/11/16 Javascript
Nodejs下用submit提交表单提示cannot post错误的解决方法
2016/11/21 NodeJs
angular 用拦截器统一处理http请求和响应的方法
2017/06/08 Javascript
PHP 实现一种多文件上传的方法
2017/09/20 Javascript
swiper动态改变滑动内容的实现方法
2018/01/17 Javascript
nodejs 生成和导出 word的实例代码
2018/07/31 NodeJs
vue路由传参的基本实现方式小结【三种方式】
2020/02/05 Javascript
[01:07]DOTA2次级职业联赛 - Fpb战队宣传片
2014/12/01 DOTA
[45:25]OG vs EG 2019国际邀请赛淘汰赛 胜者组 BO3 第一场 8.22
2019/09/05 DOTA
python自动化脚本安装指定版本python环境详解
2017/09/14 Python
Python反射和内置方法重写操作详解
2018/08/27 Python
对python 命令的-u参数详解
2018/12/03 Python
python交互界面的退出方法
2019/02/16 Python
Django 表单模型选择框如何使用分组
2019/05/16 Python
CSS3 透明色 RGBA使用介绍
2013/08/06 HTML / CSS
CSS图片翻转动画技术详解(IE也实现了)
2014/04/03 HTML / CSS
写一个用矩形法求定积分的通用函数
2012/11/08 面试题
酒店服务实习自我鉴定
2013/09/22 职场文书
数学教师求职信范文
2015/03/20 职场文书
Python Pycharm虚拟下百度飞浆PaddleX安装报错问题及处理方法(亲测100%有效)
2021/05/24 Python
8个JS的reduce使用实例和reduce操作方式
2021/10/05 Javascript