Python讀execl數據到mysql中
背景:在日常工作中,經常有運營同事有部分需求,需要講execl中的數據錄入到指定的數據庫和表中,所以此處使用腳本來讀取execl后講數據插入到mysql中,避免每次都需要手動插入的繁瑣事項。
1、表格內容
需求需要將C列,D列,E列的數據插入到對應的mysql表中。
2、整理腳本
此處使用python來整理腳本,使用到xlrd模塊和pymysql模塊,一些對xlrd用法總結:
1、打開Excel文件讀取數據 data = xlrd.open_workbook('excelFile.xls') 2、獲取一個sheet表格(任一一個均可) table = data.sheets()[0] #通過索引順序獲取 table = data.sheet_by_index(0) #通過索引順序獲取 table = data.sheet_by_name(u'Sheet1')#通過名稱獲取 4、獲取整行和整列的值(數組) table.row_values(i) table.col_values(i) 5、獲取行數和列數 nrows = table.nrows ncols = table.ncols 6、獲取某一單元格值 cell_A1 = table.cell(0,0).value cell_C4 = table.cell(3,2).value
3、表格讀取腳本(execl_runner.py)
# pip uninstall xlrd # pip install xlrd==1.2.0 #此處需按照1.2.0的xlrd模塊,高版本的去掉對了xlsx格式的支持 import xlrd from xlrd import sheet from mysql_utils import mysql_connector class read_execl(): def __init__(self,platformid,execl_name): self.platformid = platformid self.execl_name = execl_name def open_execl(self,name): file = xlrd.open_workbook(name) sheet_name = file.sheet_by_index(0) #獲取第一個sheet名稱 total_line = sheet_name.nrows return total_line,sheet_name def operation_execl(self): total_line,sheet_name = self.open_execl(self.execl_name) title = sheet_name.row_values(0) #獲取某一行的數據,從0開始 for line in range(1,total_line): iccid = sheet_name.cell(line,title.index('ICCID')).value msisdn = sheet_name.cell(line,title.index('MSISDN')).value imsi = sheet_name.cell(line,title.index('IMSI')).value values = (iccid,msisdn,imsi,self.platformid) mysql_cli = mysql_connector(values) mysql_cli.select() if __name__ == '__main__': platformid = 2 # 4G卡為2,5G卡為3 filename = r"C:\Users\wusy1\Desktop\卡導入模板-5G.xlsx" execl_obj = read_execl(platformid,filename) execl_obj.operation_execl()
4、mysql數據插入腳本
import pymysql from pymysql.cursors import DictCursor class mysql_connector(): def __init__(self,values): self.host = '172.23.xxx.xx' self.password = 'xxxxxx' self.user = 'root' self.database = 'flow' self.charset = 'utf8' self.values = values self.con,self.cursor = self.conn() def conn(self): con = pymysql.connect(host=self.host,user=self.user,passwd=self.password, database=self.database,charset=self.charset) cursor = con.cursor(cursor=DictCursor) return con,cursor def insert(self): sql = "insert into esim(iccid,msisdn,imsi,platformid) values(%s,%s,%s,%s)" self.cursor.execute(sql,self.values) self.con.commit() self.con.close() print('ccid is %s insert successful...' % self.values[0]) def select(self): sql = "select iccid,msisdn,imsi,platformid from esim where iccid = %s" flag = self.cursor.execute(sql,self.values[0]) if not flag: self.insert() else: # print('iccid is %s has exists...,msg is' % self.values[0]) result = self.cursor.fetchall() print(result)
執行結果: