【问题标题】:Datetime column gives unexpected format in python for Postgres table [duplicate]日期时间列在 python 中为 Postgres 表提供了意外的格式 [重复]
【发布时间】:2020-10-26 11:32:25
【问题描述】:

这是我试图在 python 文件中运行的查询

cur.execute(f"(select data_end_time from table;))
records = cur.fetchall()")
print(records[0])

这是我得到的输出

datetime.datetime(2020, 7, 6, 17, 0)

预期输出

2020-07-06 17:00:00

records[0] 包含值 datetime.datetime(2020, 7, 6, 17, 0) 并且是元组类型

【问题讨论】:

  • 这不能回答我的问题..
  • records[0] 包含值 datetime.datetime(2020, 7, 6, 17, 0),并且是元组类型
  • 请澄清:你的意思是它是一个内部有日期时间对象的元组吗?
  • records[0] 是元组类型,里面有日期时间
  • 你试过records[0][0].strftime('%Y-%m-%d %H:%M:%S')吗?

标签: python sql python-3.x postgresql datetime


【解决方案1】:

您必须使用 datetime.strftime 将 datetime 对象转换为字符串

import datetime

x = datetime.datetime(2020, 7, 6, 17, 0)

print(x.strftime('%Y-%m-%d %H:%M:%S')

#output
'2020-07-06 17:00:00'

【讨论】:

  • Din work ..Error :'tuple' object has no attribute 'strftime'
  • 试试 x[0]..strftime('%Y-%m-%d %H:%M:%S')
  • records[0][0].strftime('%Y-%m-%d %H:%M:%S') 工作..
猜你喜欢
  • 2019-04-02
  • 2018-03-13
  • 2017-11-07
  • 2013-04-12
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
相关资源
最近更新 更多