python 读取excel文件 效率 时间 格式_无法从excel文件中读取格式正确的日期时间值,并使用python将其保存到数据库中...

我有一段python代码,它从excel文件读取并保存到redshift数据库中。在import psycopg2

def from_redshift():

book = xlrd.open_workbook("excelfile.xlsx")

sheet = book.sheet_by_index(0)

con = psycopg2.connect(dbname='dbname', host='something.com', port=portnum, user='username', password='password')

cursor=con.cursor()

query = """INSERT INTO table_name (col1, col2, col3, start_date, update_date) VALUES (%s, %s, %s, %s, %s)"""

for r in range(1, sheet.nrows):

col1 = sheet.cell(r,0).value

col2 = sheet.cell(r,1).value

col3 = sheet.cell(r,2).value

start_date = sheet.cell(r,3).value

update_date = sheet.cell(r,4).value

# Assign values from each row

values = (col1, col2, col3, start_date, update_date)

# Execute sql Query

cursor.execute(query, values)

print("Executed")

# Close the cursor

cursor.close()

代码在读取和插入数据库时工作正常,但我的问题是,'start_date'和'update_date'字段在数据库中属于datetime,因此当我尝试插入时,错误是这两列中的值格式不正确,当我在数据库中将这两列更改为varchar时,它插入这些值是一些奇怪的数字,比如23.12345(类似的)。在

这两列中的值看起来像YYYY-MM-DD HH:MM:[SS](自定义格式)。在

如何在数据库中正确获取这些日期时间值?在

^{pr2}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值