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 用户登录验证的小例子
Mar 06 Python
ptyhon实现sitemap生成示例
Mar 30 Python
Python动态加载模块的3种方法
Nov 22 Python
Python tkinter模块弹出窗口及传值回到主窗口操作详解
Jul 28 Python
Pyqt清空某一个QTreeewidgetItem下的所有分支方法
Jun 17 Python
pyinstaller 3.6版本通过pip安装失败的解决办法(推荐)
Jan 18 Python
基于Tensorflow:CPU性能分析
Feb 10 Python
jupyter notebook tensorflow打印device信息实例
Apr 20 Python
利用Python如何实时检测自身内存占用
May 09 Python
Python爬虫获取页面所有URL链接过程详解
Jun 04 Python
Python实现Telnet自动连接检测密码的示例
Apr 16 Python
LeetCode189轮转数组python示例
Aug 05 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代码
2007/03/03 PHP
PHP的运行机制与原理(底层)
2015/11/16 PHP
给WordPress中的留言加上楼层号的PHP代码实例
2015/12/14 PHP
Linux安装配置php环境的方法
2016/01/14 PHP
利用php输出不同的心形图案
2016/04/22 PHP
详解Yii实现分页的两种方法
2017/01/14 PHP
PHP实现APP微信支付的实例讲解
2018/02/10 PHP
javaScript函数中执行C#代码中的函数方法总结
2013/08/07 Javascript
从QQ网站中提取的纯JS省市区三级联动菜单
2013/12/25 Javascript
js代码实现的加入收藏效果并兼容主流浏览器
2014/06/23 Javascript
JS实现控制表格内指定单元格内容对齐的方法
2015/03/30 Javascript
JS定时器实现数值从0到10来回变化
2016/12/09 Javascript
jQuery实现立体式数字滚动条增加效果
2016/12/21 Javascript
node文字生成图片的示例代码
2017/10/26 Javascript
vue组件发布到npm简单步骤
2017/11/30 Javascript
Vue2实时监听表单变化的示例讲解
2018/08/30 Javascript
vue 通过绑定事件获取当前行的id操作
2020/07/27 Javascript
[05:05]第三天的dota2
2013/07/29 DOTA
[00:32]2018DOTA2亚洲邀请赛OpTic出场
2018/04/03 DOTA
用python代码做configure文件
2014/07/20 Python
python 把文件中的每一行以数组的元素放入数组中的方法
2018/04/29 Python
pytorch中tensor的合并与截取方法
2018/07/26 Python
Python给定一个句子倒序输出单词以及字母的方法
2018/12/20 Python
Python实现定制自动化业务流量报表周报功能【XlsxWriter模块】
2019/03/11 Python
Python Scrapy多页数据爬取实现过程解析
2020/06/12 Python
Python使用grequests并发发送请求的示例
2020/11/05 Python
你正在寻找的CSS3 动画技术
2011/07/27 HTML / CSS
前端隐藏出边界内容的实现方法
2016/04/14 HTML / CSS
亚洲最大的运动鞋寄售店:KicksCrew
2020/11/26 全球购物
What's the difference between an interface and abstract class? (接口与抽象类有什么区别)
2012/10/29 面试题
销售主管的自我评价分享
2014/01/03 职场文书
父母对孩子的寄语
2014/04/09 职场文书
车间安全生产标语
2014/06/06 职场文书
放射科岗位职责
2015/02/14 职场文书
2015年会计人员工作总结
2015/05/22 职场文书
小学教师教学反思
2016/02/24 职场文书