各位高手大家好:在嘗試使用Python語句將 工作用excel 導(dǎo)入本機(jī)的Mysql 時,需要將"EOL By"字段被Python識別成數(shù)字。為了將其轉(zhuǎn)化成可接受的日期型的形式,我使用了xlrd.xldate_as_datetime()的方法,隨即報(bào)錯: '<' not supported between instances of 'str' and 'int' 。?我想不明白的是:分明之前使用了同樣方法的initiation date可以正常轉(zhuǎn)化成日期型顯示,而EOL By"字段就顯示報(bào)錯了以下是我的代碼:各位高手如果看出了問題煩請點(diǎn)播一下,不盛感激import xlrdimport pymysqlimport xlrd# 打開excel----------------------------------------------------------def open_excel():try:? ? ? ? book = xlrd.open_workbook('C:/Users/lenovo/Desktop/工作紀(jì)錄就靠它了.xlsx')? #文件名,把文件與py文件放在同一目錄下? ? except:? ? ? ? print("open excel file failed!")? ? try:? ? ? ? sheet = book.sheet_by_name('ANZ紀(jì)錄')? ?#execl里面的worksheet1? ? ? ? return sheet? ? except:? ? ? ? print("locate worksheet in excel failed!")# 驗(yàn)證:open_excel 是跑的通的#-----------------------------------------------------------------#連接數(shù)據(jù)庫-------------------------------------------------------try:? ? db = pymysql.connect(host='localhost',user="root",? ? ? ? passwd='QAZwsx12345678',? ? ? ? db='test',? ? ? ? charset='utf8')except:? ? print("could not connect to mysql server")# 驗(yàn)證:本機(jī)數(shù)據(jù)庫連接 是跑的通的#----------------------------------------------------------------def search_count():? ? cursor = db.cursor()? ? select = "select count(PN) from pn_infor" #獲取表中xxxxx記錄數(shù)? ? cursor.execute(select) #執(zhí)行sql語句? ? line_count = cursor.fetchone()? ? print(line_count[0])#驗(yàn)證:search_count 是可以跑通的#-----------------------------------------------------------------def insert_deta():? ? sheet = open_excel()? ? cursor = db.cursor()? ? for i in range(5, sheet.nrows): #第一行是標(biāo)題名,對應(yīng)表中的字段名所以應(yīng)該從第二行開始,計(jì)算機(jī)以0開始計(jì)數(shù),所以值是1? ? ? ? inidate = sheet.cell(i,0).value #取第i行第0列? ? ? ? initiation_date = xlrd.xldate_as_datetime(inidate,0) # 把數(shù)字型的日期解碼成真正的日期? ? ? ? requestor = sheet.cell(i,1).value#取第i行第1列,下面依次類推? ? ? ? EB? = sheet.cell(i,2).value? ? ? ? EOL_By = initiation_date = xlrd.xldate_as_datetime(EB,0) # 把數(shù)字型的日期解碼成真正的日期? ? ? ? customer_name = sheet.cell(i,3).value? ? ? ? Type_of_Service = sheet.cell(i,4).value? ? ? ? PN = sheet.cell(i,5).value? ? ? ? description30 = sheet.cell(i,6).value? ? ? ? description80 = sheet.cell(i,7).value? ? ? ? Enable = sheet.cell(i,8).value? ? ? ? CostUSD = sheet.cell(i,9).value? ? ? ? CostAUD = sheet.cell(i,10).value? ? ? ? ListpriceAUD = sheet.cell(i,11).value? ? ? ? DistipriceAUD = sheet.cell(i,12).value? ? ? ? print(initiation_date)? ? ? ? print(requestor)? ? ? ? print(EOL_By)? ? ? ? print(customer_name)? ? ? ? print(Type_of_Service)? ? ? ? print(PN)? ? ? ? print(description30)? ? ? ? print(description80)? ? ? ? print(Enable)? ? ? ? print(CostUSD)? ? ? ? print(CostAUD)? ? ? ? print(ListpriceAUD)? ? ? ? print(DistipriceAUD)? ? ? ? print("---------------")? ? ? ? value = (initiation_date,requestor,EOL_By,customer_name,Type_of_Service,PN,description30,description80,Enable,CostUSD,CostAUD,ListpriceAUD,DistipriceAUD)? ? ? ? print(value)? ? ? ? sql = "INSERT INTO pn_infor(initiation_date,requestor,EOL_By,customer_name,Type_of_Service,PN,description30,description80,Enable,CostUSD,CostAUD,ListpriceAUD,DistipriceAUD)VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"? ? ? ? cursor.execute(sql,value) #執(zhí)行sql語句? ? ? ? db.commit()? ? cursor.close() #關(guān)閉連接insert_deta()db.close()#關(guān)閉數(shù)據(jù)print ("ok ")以下是要導(dǎo)入的excel 的部分信息(網(wǎng)站不能添加附件,很遺憾)這個是遭遇的報(bào)錯截圖
添加回答
舉報(bào)
0/150
提交
取消