python循环打开EXCEL文档,向mysql写入数据,开始很快,后来越来越慢怎么处理?

新手上路,请多包涵

我这里有个需求,是读取30个表格的内容,然后写入数据库,我使用的是for循环打开这些表格,如果使用for循环的话,开始很快,后来会越来越慢。
单次运行的话速度也很快,读取一个表格写入数据库的时间大概在200s左右。请问一下,这个是什么问题?如何解决?
mysql中的d建立了索引

import pymysql
from openpyxl import load_workbook
def read_excel(p):
    # p = '26'
    db = pymysql.Connect(host="localhost",port=3306,user="test",passwd="123456",db="test",charset="utf8")    
    cur = db.cursor()                
    excel = r'E:\整理数据\4月\1 ({}).xlsx'.format(p)
    wb = load_workbook(excel)
    ws = wb.active
    rows = ws.max_row
    #print(rows)
    for i in range(2,rows):
        d = ws['G%s' %i].value
        fb = ws['F%s' %i].value
        kh = ws['BB%s' %i].value
        wdzl = ws['AV%s' %i].value
        zxzl = ws['AW%s' %i].value
        jpzl = ws['AU%s' %i].value
        jszl = ws['AT%s' %i].value
        jpinfo = ws['AX%s' %i].value
        ywtime = ws['J%s' %i].value
        try:
 
            sql = "select dh from testdata where d = '{}'".format(d)
            cur.execute(sql)
            tid = cur.fetchone()
            if tid == None:
                print('新数据')
                # 向数据库写入新数据
                input_sql = "insert into testdata(d,fb,kh,wdzl,zxzl,jpzl,jszl,jpinfo,ywtime) values('{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(d,fb,kh,wdzl,zxzl,jpzl,jszl,jpinfo,ywtime)
                cur.execute(input_sql)
                db.commit()
                print(i,d,fb,wdzl,zxzl,jpzl,jszl,jpinfo,ywtime)
            else:
                print('数据已存在')
                sql1 = "select wdzl,zxzl,jszl,jpzl,jpinfo from testdata where d = '{}'".format(d)
                cur.execute(sql1)
                w = cur.fetchone()
                print(w)
                    
                if wdzl != '0' and w[0] == '0':
                    up_sql = "update testdata set wdzl = {} where d = '{}'".format(wdzl,d)
                    cur.execute(up_sql)
                    db.commit()
                else:
                    pass
                if zxzl != '0' and w[1] == '0':
                    up_sql = "update testdata set zxzl = '{}' where d = '{}'".format(zxzl,d)
                    cur.execute(up_sql)
                    db.commit()
                else:
                    pass
                if jszl != '0' and w[2] == '0':
                    up_sql = "update testdata set jszl = '{}' where d = '{}'".format(jszl,d)
                    cur.execute(up_sql)
                    db.commit()
                else:
                    pass
                if jpzl != '0' and w[3] == '0':
                    up_sql = "update testdata set jpzl = '{}' where d = '{}'".format(jpzl,d)
                    cur.execute(up_sql)
                    db.commit()
                else:
                    pass
                if jpinfo != '0' and w[4] == '0':
                    up_sql = "update testdata set jpinfo = '{}' where d = '{}'".format(jpinfo,d)
                    cur.execute(up_sql)
                    db.commit()
                else:
                    pass
            print(d)
        except Exception as e:
            print(e,d)
    print('-'*50)
    print(p,'号数据已完成')
    cur.close()
    db.close()
 
# for i in range(4,31):
#     read_excel(i)
read_excel('17')
 
阅读 3.9k
3 个回答
✓ 已被采纳新手上路,请多包涵

不要print,这个速度慢可能是因为print

openpyxl似乎不会主动释放内存,每个文件读取完后,手动释放内存试一下:

import gc

...

del wb,ws
gc.collect()

推测的话,openpyxl的根据行号列号读取的时候,是从第一行第一列开始遍历,直到行号等于指定行号,列号等于指定列号,所以要读取的行号列号越多就越慢,(也可能是从第一个有数据的行或列),而xlrd则是类似与数组一样,我们要取第几个元素,直接根据下标找到内存中对应地址的元素即可,所以无论excel总量多少,速度基本都是不变的。

楼上说的也不错,可以试试。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题