91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 25.35MB
    文件類型: .rar
    金幣: 2
    下載: 1 次
    發(fā)布日期: 2023-06-30
  • 語言: Python
  • 標簽: python??excel??mysql??

資源簡介

使用python3導入excel文件到mysql。資源包含(excel文件,python源碼,建表語句),使用多線程,一分鐘一萬條的速度(無大字段)

資源截圖

代碼片段和文件信息

import?pymysql?#?操作mysql的模塊
import?openpyxl?#?xlsx格式對應的操作模塊
import?time
import?threadpool?#?線程池模塊
import?math
from?datetime?import?datetime

successList?=?[]?#?儲存每個線程成功的數(shù)目,用于統(tǒng)計

def?readRow(rows):

conn?=?pymysql.connect(host=“192.168.0.243“port=3306user=“root“passwd=“root“db=“mydb“charset=“utf8“)
cur?=?conn.cursor()?#?獲取游標

num?=?0
for?row?in?rows:

itemNo?=?row[0].value?if?row[0].value?!=?None?else?111
itemName?=?row[1].value.replace(“‘“““)?if?row[1].value?!=?None?else?““
itemName?=?itemName.replace(“\\“?“|“)
pym?=?row[2].value.replace(“‘“““)?if?row[2].value?!=?None?else?““
pym?=?pym.replace(“\\“?“|“)
itemSize?=?row[3].value.replace(“‘“““)?if?row[3].value?!=?None?else?““
itemSize?=?itemSize.replace(“\\“?“|“)
unitNo?=?row[4].value.replace(“‘“““)?if?row[4].value?!=?None?else?““
unitNo?=?unitNo.replace(“\\“?“|“)
productArea?=?row[5].value.replace(“‘“““)?if?row[5].value?!=?None?else?““
productArea?=?productArea.replace(“\\“?“|“)

args?=?(itemNoitemNamepymitemSizeunitNoproductArea)
try:
sql?=?r‘‘‘
insert?into?bar_code_dcm1?(itemNoitemNamepymitemSizeunitNoproductArea)
values
(%s‘%s‘‘%s‘‘%s‘‘%s‘‘%s‘)?
‘‘‘?%?args
#?print(sql?“\r\n----------------------------------------------------------“)
cur.execute(sql)
conn.commit()
num?=?num?+1
except?Exception?as?e:
print(Exception?e“SQL:%s?“?%?sql)
else:
pass
finally:
pass

if?num?%?1000?==?0:
print(“---當前線程已導入:“?num“?條???%s“?%?time.strftime(“%Y-%m-%d?%H:%M:%S“?time.localtime())?)
successList.append(num)
conn.close()
#?time.sleep(1)


def?excel2Mysql(excelFileName):

wb?=?openpyxl.load_workbook(excelFileName)??#打開excel文件?;16.5s
sheetList?=?wb.get_sheet_names()#獲取工作簿所有工作表名

for?sheetName?in?sheetList:?#?遍歷,每一個工作簿

sheetObj?=?wb.get_sheet_by_name(sheetName)?#獲取工作簿對象
rows?=?sheetObj.iter_rows()

bigList?=?[]?#?每個元素為一行excel表格內(nèi)容
poolArgsList?=?[]?#?每個元素為一萬行excel表格內(nèi)容傳遞給線程池的集合

for?row?in?rows:
if?len(row[0].value)?!=?13:?#?過濾掉不標準的條形碼數(shù)據(jù)(標準數(shù)字條形碼長度為13)
continue
else:
bigList.append(row)

cycle?=?math.ceil(len(bigList)?/?10000)
for?index?in?range(1cycle+1):
thisList?=?bigList[(index-1)*10000:index*10000]?#?list切片
poolArgsList.append(thisList)

pools?=?threadpool.ThreadPool(10)??#?初始化10個線程(不一定全用上,python會自己調(diào)度,最好是1w數(shù)據(jù)對應1個線程)
print(“-*-數(shù)據(jù)讀取,組裝完畢-*-*-*開啟?%d?個線程-*-*-*-?\n\r“?%?10)
tasks?=?threadpool.makeRequests(readRow?poolArgsList)?#?創(chuàng)建任務(處理函數(shù),可迭代對象),每一個迭代元素即為處理函數(shù)的參數(shù)
[pools.putRequest(task)?for?task?in?tasks]?#?線程池和任務都有了,將任務放入線程池中,執(zhí)行
pools.wait()

wb.close()

if?__name__?==?‘__main__‘:
startTime?=?datetime.now()
excelFileName?=?“C:/Users/xusanduo/Desktop/excelData/50-55w.xlsx“
print(“[??%s??]?[?開始導入?%s?“?%?(?time.strftime(“%Y-%m-%d?%H:%M:%S“?time.localtime()?)?excelFileName)“文件?]“?)
excel2Mysql(excelFileName)
endTime?=?datetime.now()
print(?“[??%s??]?%s?

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----

?????文件???26862687??2017-06-30?14:57??導入excel內(nèi)容到mysql例子\1-54w.xlsx

?????文件???????3667??2017-08-31?13:52??導入excel內(nèi)容到mysql例子\excelToMysql.py

?????文件????????948??2017-11-08?10:02??導入excel內(nèi)容到mysql例子\表結構.txt

?????目錄??????????0??2017-11-08?10:03??導入excel內(nèi)容到mysql例子

-----------?---------??----------?-----??----

?????????????26867302????????????????????4


評論

共有 條評論