将数据库查询转换为有用的格式

时间:2018-12-19 15:09:34

标签: python postgresql pandas pgadmin

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

(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()

1 个答案:

答案 0 :(得分:1)

根据Python-to-Postgres types table psycopg2's文档,这是预期的行为。 Postgres的timestamp在Python中成为datetime对象。

然后,如果您确实需要时间戳记值,则只需在datetime.utcfromtimestamp()对象上调用datetime即可: