# -*- 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()