python把excel导入mysql报错_使用Python 将excel 导入Mysql 时遭遇报错: '<'..._慕课猿问...

各位高手大家好:

在尝试使用Python语句将 工作用excel 导入本机的Mysql 时,需要将"EOL By"字段被Python识别成数字。

为了将其转化成可接受的日期型的形式,我使用了xlrd.xldate_as_datetime()的方法,随即报错: '

我想不明白的是:分明之前使用了同样方法的initiation date可以正常转化成日期型显示,而EOL By"字段就显示报错了

以下是我的代码:各位高手如果看出了问题烦请点播一下,不盛感激

import xlrd

import pymysql

import 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值