内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Python如何批量刪除只保留最近幾天table-創(chuàng)新互聯(lián)

小編給大家分享一下Python如何批量刪除只保留最近幾天table,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

為東方等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及東方網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、東方網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

Python批量刪除table,只保留最近幾天的table

代碼如下:

#!/usr/bin/python3
"""
批量刪除table,只保留最近幾天的table
"""
import pymysql
import re
def conn_(host='',usr='',passwd='',db='',port=3306,):
  conn = pymysql.connect(host, usr, passwd, db, port,charset='utf8')
  return conn
def del_table(conn_,table_pre='',table_suff='%Y%m%d',keep_count=3):
  date_form = None
  if table_suff == "%Y%m%d":
    date_form = "_(\d{4}\d{1,2}\d{1,2})$"
    date_len = 8
  elif table_suff == "%Y-%m-%d":
    date_form = "_(\d{4}-\d{1,2}-\d{1,2})$"
    date_len = 10
  elif table_suff == "%Y%m":
    date_form = "_(\d{4}\d{1,2})$"
    date_len = 6
  elif table_suff == "%Y-%m":
    date_form = "_(\d{4}-\d{1,2})$"
    date_len = 7
  else:
    raise Exception("暫時(shí)不支持其他類型的時(shí)間后綴")
  curs = conn_.cursor()
  curs.execute('SHOW TABLES')
  data = curs.fetchall()
  table_ = r'%s'%table_pre+date_form
  list_table = []
  i = 0
  for table in data:
    mt = re.search(table_, table[0])
    if mt:
      if len(mt.groups()[0]) == date_len:
        list_table.append((table[0], mt.groups()[0]))
        i += 1
  sorted(list_table, key=lambda date: date[1]) #按照表結(jié)構(gòu)后綴時(shí)間升序排序
  for j in range(i-keep_count):
    sql = 'DROP TABLE if exists %s'%list_table[j][0]
    curs.execute(sql)
  curs.close()
  conn_.close()
if __name__ == '__main__':
  table_pre = "tree_product"
  table_suff = "%Y%m%d"
  # table_suff = "%Y-%m-%d"
  # table_suff = "%Y%m"
  # table_suff = "%Y-%m"
  conn=conn_('10.0.0.11','root','sctele@root','sxf',port=3306)
  del_table(conn,table_pre=table_pre,table_suff=table_suff,keep_count=1)

看完了這篇文章,相信你對(duì)“Python如何批量刪除只保留最近幾天table”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站標(biāo)題:Python如何批量刪除只保留最近幾天table-創(chuàng)新互聯(lián)
路徑分享:http://www.rwnh.cn/article10/ppcgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、移動(dòng)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、App開發(fā)云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)
宝丰县| 甘洛县| 平顺县| 蕉岭县| 固安县| 弥勒县| 桂平市| 东乡族自治县| 富蕴县| 绥滨县| 恩施市| 连山| 新宁县| 镇安县| 双江| 隆回县| 横峰县| 武乡县| 鸡泽县| 任丘市| 苏尼特右旗| 临沭县| 乾安县| 黄陵县| 阿克陶县| 泰宁县| 宁强县| 河间市| 新昌县| 铁岭市| 瓦房店市| 司法| 新和县| 泾川县| 武宣县| 株洲县| 镇坪县| 图们市| 文成县| 民丰县| 赤峰市|