国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python3 使用openpyxl將mysql數(shù)據(jù)寫(xiě)入xlsx的操作

瀏覽:3日期:2022-07-27 18:01:48

編程的生活愈發(fā)不容易了,工作越來(lái)越難找,說(shuō)多了都是淚還是給大家貢獻(xiàn)些代碼比較實(shí)際。

python3 鏈接數(shù)據(jù)庫(kù)需要下載名為pymysql的第三方庫(kù)

python3 讀寫(xiě)xlsx需要下載名為openpyxl的第三方庫(kù)

在此我只貢獻(xiàn)鏈接數(shù)據(jù)庫(kù)和寫(xiě)入xlsx的代碼

import pymysql.cursorsfrom fj.util import loggerfrom openpyxl import Workbookfrom openpyxl.compat import rangefrom openpyxl.utils import get_column_letter # 鏈接數(shù)據(jù)庫(kù)的游標(biāo)connect = pymysql.Connect( host='localhost', port=3306, user=’root’, passwd=’123456’, db=’zyDB’, charset=’utf8’,)cursor = connect.cursor() # 關(guān)閉數(shù)據(jù)庫(kù)鏈接操作def clos_cursor(): return cursor.close(); # 讀取數(shù)據(jù)庫(kù)數(shù)據(jù)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(); # 關(guān)閉數(shù)據(jù)庫(kù)鏈接操作def clos_cursor(): cursor.close(); connect.close() def read_mysql_to_xlsx(): #要?jiǎng)?chuàng)建的xlsx名稱(chēng) dest_filename = ’jb_data.xlsx’ wb = Workbook() ws1 = wb.active ws1.title = 'fj_date' # 列名 ws1.cell(row=1,column=1,value='fj_id(數(shù)據(jù)庫(kù)編號(hào))') ws1.cell(row=1,column=2,value='jb_id(疾病編號(hào))') ws1.cell(row=1,column=3,value='mj_id(名醫(yī)編號(hào))') ws1.cell(row=1,column=4,value='fj_name(方劑名稱(chēng))') 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(方劑治驗(yàn))') 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(臨床應(yīng)用)') ws1.cell(row=1,column=20,value='tjbq(推薦標(biāo)簽)') ws1.cell(row=1,column=21,value='zysx(注意事項(xiàng))') ws1.cell(row=1,column=22,value='fjzb(制備方法)') ws1.cell(row=1,column=23,value='fg(方歌)') ws1.cell(row=1,column=24,value='path(路徑)') # 循環(huán)數(shù)據(jù)寫(xiě)入內(nèi)容 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]) # 創(chuàng)建xlsx wb.save(filename=dest_filename) if __name__ == ’__main__’: read_mysql_to_xlsx()

補(bǔ)充知識(shí):Python 關(guān)閉文件釋放內(nèi)存的疑惑

我用with語(yǔ)句打開(kāi)了一個(gè)4g的文件讀取內(nèi)容,然后程序末尾設(shè)置一個(gè)死循環(huán),按理說(shuō)with語(yǔ)句不是應(yīng)該自動(dòng)關(guān)閉文件釋放資源嗎?

但是系統(tǒng)內(nèi)存一直沒(méi)有釋放。應(yīng)該是被文件讀取到的變量content一直占用嗎?把content刪除就會(huì)釋放內(nèi)存。或者去掉死循環(huán),程序退出資源就自動(dòng)釋放了

既然這樣的話(huà)關(guān)閉文件貌似沒(méi)啥作用呢?具體釋放了什么資源?

Python一直占用著將近5G的內(nèi)存:

python3 使用openpyxl將mysql數(shù)據(jù)寫(xiě)入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 syswith open(r’H:BaiduNetdiskDownload4K.mp4’,’rb’) as f: print(f.closed) content=f.read()print(f.closed)print(sys.getrefcount(f))while True: pass

以上這篇python3 使用openpyxl將mysql數(shù)據(jù)寫(xiě)入xlsx的操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 成人免费大片黄在线观看com | 美女黄页网站免费进入 | 精品午夜国产在线观看不卡 | 国内精自线一二区 | 成人午夜影院在线观看 | 欧美性高清视频免费看www | fc2久久 | 性盈盈影院影院 | 国产成人精品男人的天堂网站 | 国模肉肉人体大尺度啪啪 | 亚洲在线网 | 她也啪在线视频 | 视频久久精品 | 欧美一级高清视频在线播放 | 成人黄色免费看 | 欧美搞黄视频 | 精品久久久久久久久久香蕉 | 国内自拍在线 | 久久er国产精品免费观看1 | 2020国产精品 | 日韩欧美一区二区在线观看 | 久久精品国产精品亚洲精品 | 毛片免费观看网址 | 午夜看片网站 | 特色毛片| 久色tv| 一区二区视频在线 | 国产香蕉久久 | 亚洲成人在线免费视频 | 国产成人精品久久一区二区三区 | 久艹在线| 一级视频在线 | 国产精品亚洲第一区二区三区 | 国产在线小视频 | 日韩一级欧美一级一级国产 | 欧美成人精品在线 | 99久久精品久久久久久婷婷 | 亚洲欧美视频网站 | 97久久精品一区二区三区 | www.色黄| 日本视频免费在线播放 |