首页 > 解决方案 > 将数据库查询转换为有用的格式

问题描述

我的数据库上有一个表,我获取了表的第一行,结果如下所示:

(datetime.datetime(2018, 10, 3, 16, 27, 29, 198000), 'buy', Decimal('0.00276851'), Decimal('6.00000000'), 1, None)

但是,在我的数据库(datetime.datetime(2018, 10, 3, 16, 27, 29, 198000)上是一个时间戳,但这里是一个字符串。如何将此结果转换为我以后可以在我的代码中使用的时间戳格式?谢谢!

到目前为止,这是我的代码:

conn = psycopg2.connect("dbname=monty user=postgres host=localhost password=*****")
cur = conn.cursor()
cur.execute("SELECT * FROM binance.zrxeth_aggregated FETCH first 1 rows only;")
row = cur.fetchone()
cur.close()
conn.close()

标签: pythonpostgresqlpandaspgadmin

解决方案


根据Python-to-Postgres 类型表 psycopg2 的文档,这是预期的行为。Postgrestimestamp成为datetimePython 中的对象。

然后,如果您真的需要时间戳值,只需调用datetime.utcfromtimestamp()您的datetime对象即可:


推荐阅读