zoukankan      html  css  js  c++  java
  • 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')
  • 相关阅读:
    CodeForces 7B
    CodeForces 4D
    离散化
    线段树入门
    洛谷 P3951 小凯的疑惑(赛瓦维斯特定理)
    Codeforces 1295D Same GCDs (欧拉函数)
    Codeforces 1295C Obtain The String (二分)
    Codeforces 1295B Infinite Prefixes
    Codeforces 1295A Display The Number(思维)
    Codeforces 1294F Three Paths on a Tree(树的直径,思维)
  • 原文地址:https://www.cnblogs.com/xingxiz/p/9907791.html
Copyright © 2011-2022 走看看