• excel表格用协程插入到mysql


    #!/usr/bin/python
    #coding=utf-8
    
    import MySQLdb
    import gevent
    import xlrd
    import sys
    
    
    class mysqldb:
        def __init__(self,host,username,password,db,charset='utf8'):
            self.host = host          
            #self.port = port          
            self.username = username  
            self.password = password  
            self.db = db              
            self.charset = charset    
            self.connect()
    
        def connect(self):
            self.conn = MySQLdb.connect(host=self.host,
                                        #port=self.port,
                                        user=self.username,
                                        passwd=self.password,
                                        db=self.db,
                                        charset=self.charset
                                      )
            self.asynchronous(sys.argv[1])
    
        def run(self,xxlist):
            self.cur = self.conn.cursor()
            sql = "insert into ENQD(dstip,dstloc,dstisp,srcip,srcloc,srcisp,status) VALUES (%s,%s,%s,%s,%s,%s,%s)"
            excutesqls = self.cur.executemany(sql,xxlist)
            self.conn.commit()
    
        def asynchronous(self,file):
            data_list = []
            book = xlrd.open_workbook(file)
            sheet = book.sheets()[0] 
            for r in range(1,sheet.nrows):
                dstip  =  sheet.cell(r,0).value
                dstloc =  sheet.cell(r,1).value
                dstisp =  sheet.cell(r,2).value
                srcip  =  sheet.cell(r,3).value
                srcloc =  sheet.cell(r,4).value
                srcisp =  sheet.cell(r,5).value
                status =  sheet.cell(r,6).value
                values = (dstip.encode("utf-8"), dstloc.encode("utf-8"), dstisp.encode("utf-8"), srcip.encode("utf-8"), srcloc.encode("utf-8"), srcisp.encode("utf-8"), int(status))
                data_list.append(values)
            g_l = [gevent.spawn(self.run,data_list[i:i+100])for i in range(0,len(data_list),100)]
            gevent.joinall(g_l)
            print "插入%s条数据" % (sheet.nrows-1)
            self.cur.close()
            self.conn.close() 
    
    if __name__ == '__main__':
        if not sys.argv:
            print "Please provide file path as parameter!"
            exit(1)
        else:
            t = mysqldb('localhost', 'root', '123456', 'xiao')
  • 相关阅读:
    basis 文档
    profile default1
    profile default
    2101244
    Linux下对lvm逻辑卷分区大小的调整(针对xfs和ext4不同文件系统)
    1816647
    lvm管理:扩展lv、删除pv、lv等
    HPUX and AIX SSH 互信
    SLD Related Gateway Serivces Unavaliable
    [原创]K8 MSF Bind Shell TCP 连接工具
  • 原文地址:https://www.cnblogs.com/xingxiz/p/9907791.html
Copyright © 2020-2023  润新知