Python读取execl数据写入到mysql

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)

执行结果:

原文地址:https://www.cnblogs.com/wushaoyu/p/15584682.html