python - 将数据库查询转换为有用的格式
问题描述
我的数据库上有一个表,我获取了表的第一行,结果如下所示:
(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()
解决方案
根据Python-to-Postgres 类型表 psycopg2 的文档,这是预期的行为。Postgrestimestamp
成为datetime
Python 中的对象。
然后,如果您真的需要时间戳值,只需调用datetime.utcfromtimestamp()
您的datetime
对象即可:
推荐阅读
- javascript - 使用 Croppie.js 和 Modal 裁剪图像时出现未对齐和黑色/空白上传
- wordpress - 在 Themosis 框架中创建 Wordpress “独立”插件
- mysql - 在数据库中添加具有百分比的相似数据字段
- php - Selinux:type=anom_abend for php-fpm,访问应用程序时出现错误 502
- .htaccess - Codeigniter 3 - CORS 策略:请求的资源上不存在“Access-Control-Allow-Origin”标头
- python - 一次将两个 JSON 文件转换为 pandas.DataFrame -> ValueError: Invalid file path or buffer object type:
- python - 当我单击删除按钮时,我有此错误 delete() 缺少 1 个必需的位置参数:'id'
- android - 有没有办法在回调中返回值并结束函数?
- postgresql - postgres 13 的 csvlog 似乎已损坏
- java - 如何检查为什么我在使用 nginx 和 spring boot 应用程序时出现 401 错误?