python 解析excel 插入mysql

# -*- coding: utf-8 -*-
import os
import xlrd
import datetime
import json
import sys
import re
import pymysql
import pandas as pd
parent_path = os.path.dirname(sys.path[0])
if parent_path not in sys.path:
    sys.path.append(parent_path)

today = datetime.date.today().strftime("%Y-%m-%d")

class Convert():
    def __init__(self):
        pass

    def getdate(self, date):
        s_date = datetime.date(1899, 12, 31).toordinal() - 1
        if isinstance(date, float):
            date = int(date)
            d = datetime.date.fromordinal(s_date + date)
        return d.strftime("%Y-%m-%d")

    def clean(self):
        self.dba = pymysql.connect(host='192.168.0.21', user='abc', passwd='1233333',
                                       db='exce', port=3306, charset='utf8',
                                       cursorclass=pymysql.cursors.DictCursor, connect_timeout=7200)
        self.cursor = self.dba.cursor()

        exit_sql = """SELECT count(*) as count from data where M='{0}' and `D`='{1}'"""
        update_sql = """update data set `P`='{0}' where Z='{1}' and `S`='{2}' """
        insert_sql = """
                    insert into `data_` (`Z` ,`ZM` ,`S` ,`P` )
                    values (
                    '{0}','{1}','{2}','{3}'
                    ) """

        files = ["1.xlsx", "2.xlsx"]       
        for file in files:
            sheets = {  "1.xlsx": 'a,bc,c',
                        }
            names = str(sheets[file]).split(',')
            wb = xlrd.open_workbook(file, encoding_override='gb18030')
            for name in names:
                sheet = wb.sheet_by_name(name)
                rows = sheet.nrows
                columns = sheet.ncols
                # 逐列清洗
                for r in range(1, rows):                    
                    for c in range(2, columns):
                        C = str(sheet.cell(r, 1).value)
                        D = str(sheet.cell(0, c).value)
                        M = str(sheet.cell(r, 0).value)
                        P = str(sheet.cell(r, c).value)
                        try:
                            self.cursor.execute(exit_sql.format(Z, S))
                            rows = self.cursor.fetchall()
                            count = rows[0]['count']
                            if count == 0:
                                self.cursor.execute(insert_sql.format(Z, M, D, P))
                                self.dba.commit()
                            else:
                                self.cursor.execute(
                                    update_sql.format(P, Z, S))
                                self.dba.commit()
                        except Exception as e:
                            print(e)

if __name__ == '__main__':
    statis = Convert()
    statis.clean()
    statis.cursor.close()
    statis.dba.close()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值