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