python3 使用openpyxl将mysql数据写入xlsx的操作
作者:花有清香 发布时间:2024-01-25 14:58:23
编程的生活愈发不容易了,工作越来越难找,说多了都是泪还是给大家贡献些代码比较实际。
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的内存:
官方文档:
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
来源:https://blog.csdn.net/yang_ping_cai_niao/article/details/83896367


猜你喜欢
- 为了找到matplotlib在两个点之间连线的方法真是费了好大功夫,本文主要介绍了 matplotlib绘制两点间连线的几种方法,
- 同伪类的方式类似,伪元素通过对插人到文档中的虚构元素进行触发,从而达到某种效果。在CSS1里,有两个伪元素,即:first-letter和f
- 最近写毕业设计遇到一个问题,就是我从一个txt文件中逐个读取字符,并修改其中的内容后存到另一个txt文件中,如下图:字符替换规则是把所有的0
- 关于Python 黑魔法 metaclass 的两种极端观点:这种特性太牛逼了,是无所不能的阿拉丁神灯,必须找机会用上才能显示自己的 Pyt
- 此文从以下几个方面来整理关于分区表的概念及操作:1.表空间及分区表的概念2.表分区的具体作用3.表分区的优缺点4.表分区的几种类型及操作方法
- 本文是 OpenCV图像视觉入门之路的第7篇文章,本文详细的进行了图像的缩放 cv2.resize()、旋转 cv2.flip()、平移 c
- 当获取FileField数据时会出现编码问题在数据库里显示的是D:\python项目\wxmkczpy\uploadfile\QQ截图201
- 泰勒展开与e的求法大家伙儿知道计算机里的 e是怎么求出来的吗?这还要从神奇的泰勒展开讲起……简单
- 前言scikit-learn是Python中最流行的机器学习库之一,它提供了各种各样的机器学习算法和工具,包括分类、回归、聚类、降维等。sc
- 前言删除表,大家下意识想到的命令可能是直接使用DROP TABLE "表名",这是初生牛犊的做法,因为当要删除的表达空间
- 1.说明redis作为一个缓存数据库,在各方面都有很大作用,Python支持操作redis,如果你使用Django,有一个专为Django搭
- 一:安装MySQL-python驱动 pip install mysql二:连接到MySQL服务器的test数据库#!/usr/
- 引子之前clubot使用的pyxmpp2的默认mainloop也就是一个poll的主循环,但是clubot上线后资源占用非常厉害,使用str
- 通常能听到的答案是使用了NULL值的列将会使索引失效,但是如果实际测试过一下,你就知道IS NULL会使用索引.所以上述说法有漏洞.着急的人
- “博客就像一本书”这话其实几个月前深圳FB时就有扯到,这也不是什么新概念,也许本身就应该是这样。打个比方,当你拿到一本未看过的书时,理论上你
- 导语泡泡王国 欢乐多多咕噜噜,吹泡泡,七彩泡泡满天飘。大的好像彩气球,小的就像紫葡萄。当泡泡漫天飞舞时,大朋友、小朋友都会情不自禁地被它吸引
- Python字符串处理学习中,有一道简单但很经典的题目,按照单词对字符串进行反转,并对原始空格进行保留: 如:‘ I love China!
- 配置可能会随官方改变,本文仅供参考。1.下载安装GO的包到https://code.google.com/p/go/downloads/li
- 先看看单条 SQL 语句的分页 SQL 吧。 方法1: 适用于 SQL Server 2000/2005 SELECT TOP 页大小 *
- 最近,随着数据库的日益庞大,本来两个差不多的数据库,我开始发现我的数据库查询起来越来越比我朋友网站的慢了,经过初步对照,问题好像出在访问记录