最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年05月15日 15:30:24   作者:花有清香  
這篇文章主要介紹了python3 使用openpyxl將mysql數(shù)據(jù)寫入xlsx的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

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

python3 讀寫xlsx需要下載名為openpyxl的第三方庫

在此我只貢獻(xiàn)鏈接數(shù)據(jù)庫和寫入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
 
# 鏈接數(shù)據(jù)庫的游標(biāo)
connect = pymysql.Connect(
  host="localhost",
  port=3306,
  user='root',
  passwd='123456',
  db='zyDB',
  charset='utf8',
)
cursor = connect.cursor()
 
# 關(guān)閉數(shù)據(jù)庫鏈接操作
def clos_cursor():
  return cursor.close();
 
# 讀取數(shù)據(jù)庫數(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ù)庫鏈接操作
def clos_cursor():
   cursor.close();
   connect.close()
 
def read_mysql_to_xlsx():
 
  #要創(chuàng)建的xlsx名稱
  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ù)庫編號)")
  ws1.cell(row=1,column=2,value="jb_id(疾病編號)")
  ws1.cell(row=1,column=3,value="mj_id(名醫(yī)編號)")
  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(方劑治驗(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ù)寫入內(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ǔ)充知識:Python 關(guān)閉文件釋放內(nèi)存的疑惑

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

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

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

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

官方文檔:

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數(shù)據(jù)寫入xlsx的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python?Streamlit制作交互式可視化網(wǎng)頁應(yīng)用實(shí)例

    Python?Streamlit制作交互式可視化網(wǎng)頁應(yīng)用實(shí)例

    這篇文章主要為大家介紹了Python?Streamlit制作交互式可視化網(wǎng)頁應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • 詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等)

    詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstac

    這篇文章主要介紹了詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Python 批量讀取文件中指定字符的實(shí)現(xiàn)

    Python 批量讀取文件中指定字符的實(shí)現(xiàn)

    這篇文章主要介紹了Python 批量讀取文件中指定字符的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Python筆記之facade模式

    Python筆記之facade模式

    這篇文章主要為大家詳細(xì)介紹了Python筆記之facade模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • 無需壓縮軟件,用python幫你操作壓縮包

    無需壓縮軟件,用python幫你操作壓縮包

    這篇文章主要介紹了如何用python幫你操作壓縮包,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-08-08
  • Python合并列表、字典、字符串、CSV文件、多文件技巧

    Python合并列表、字典、字符串、CSV文件、多文件技巧

    在 Python 中,有多種方法可以實(shí)現(xiàn)數(shù)據(jù)合并,無論是合并列表、合并字典、合并字符串、合并CSV文件還是合并多個文件夾中的文件,都可以使用簡單而強(qiáng)大的Python技巧來實(shí)現(xiàn),通過合并數(shù)據(jù),可以更方便地進(jìn)行數(shù)據(jù)處理和分析
    2024-03-03
  • 詳解Python讀取yaml文件多層菜單

    詳解Python讀取yaml文件多層菜單

    這篇文章主要介紹了Python讀取yaml文件多層菜單,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Python實(shí)現(xiàn)視頻畫質(zhì)增強(qiáng)的示例代碼

    Python實(shí)現(xiàn)視頻畫質(zhì)增強(qiáng)的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Python語言實(shí)現(xiàn)對視頻進(jìn)行畫質(zhì)增強(qiáng)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下
    2022-04-04
  • Python遞歸時間復(fù)雜度

    Python遞歸時間復(fù)雜度

    這篇文章主要介紹了Python遞歸時間復(fù)雜度,時間復(fù)雜度一般認(rèn)為O(logn),但遞歸算法的時間復(fù)雜度本質(zhì)上是要看遞歸的次數(shù),每次遞歸中的操作次數(shù),下面文章詳細(xì)介紹,需要的朋友可以參考一下
    2022-03-03
  • 將數(shù)據(jù)集制作成VOC數(shù)據(jù)集格式的實(shí)例

    將數(shù)據(jù)集制作成VOC數(shù)據(jù)集格式的實(shí)例

    今天小編就為大家分享一篇將數(shù)據(jù)集制作成VOC數(shù)據(jù)集格式的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02

最新評論

故城县| 西充县| 田阳县| 宣威市| 横山县| 宁安市| 洞头县| 凤山市| 天全县| 辽阳市| 南郑县| 井冈山市| 闵行区| 灌南县| 红桥区| 开平市| 儋州市| 连城县| 乐清市| 晴隆县| 石楼县| 晋江市| 昭平县| 东宁县| 杂多县| 昌都县| 昂仁县| 石棉县| 温州市| 香格里拉县| 新密市| 河东区| 分宜县| 兰西县| 芒康县| 仁怀市| 平远县| 光泽县| 平乐县| 平江县| 南江县|