我仍然是python的新手(正在使用3.x版)
我的数据库表中有数据。其中一列是date_created ..当我查询数据并将输出写入json文件时,输出是这样的
'date_created': datetime.datetime(2020, 4, 13, 7, 35, 0, 755315)
有没有一种方法可以在写入json文件之前格式化此字段?
像
字段是"date_created"
...我们应该如何在python 3中格式化它?...或者烧瓶中有办法吗?(实际上是使用烧瓶)
我想在下面看到这样的输出
'date_created': '2020-04-13 07:35:00'
使用date_create.iso_format()
。
对于UTC(这是推荐的方式):
$ python -c 'import datetime; print(datetime.datetime.utcnow().isoformat());'
2020-04-13T08:45:38.623199
没有时区信息的本地时区时间戳(默认行为,不推荐)
$ python -c 'import datetime; print(datetime.datetime.now().isoformat());'
2020-04-13T14:12:34.181240
对于时区字符串:
$ python -c 'from datetime import datetime, timezone;print(datetime.now(timezone.utc).astimezone().isoformat())'
2020-04-13T14:14:36.780896+05:30
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句