在Python中为SQL格式化某些日期

时间:2018-01-31 06:41:32

标签: python json postgresql date datetime

我有一些来自JSON文件的日期,日期和时间的字符串版本,并且想知道如何格式化它们。

我想格式化每一个,以便在PostgreSQL中轻松使用

这是第一个:

{"time": {"Friday": {"20:00": 2, "19:00": 1, "22:00": 10, "21:00": 5, "23:00": 14, "0:00": 2, "18:00": 2}, "Thursday": {"23:00": 1, "0:00": 1, "19:00": 1, "18:00": 1, "16:00": 2, "22:00": 2}, "Wednesday": {"17:00": 2, "23:00": 3, "16:00": 1, "22:00": 1, "19:00": 1, "21:00": 1}, "Sunday": {"16:00": 2, "17:00": 2, "19:00": 1, "22:00": 4, "21:00": 4, "0:00": 3, "1:00": 2}, "Saturday": {"21:00": 4, "20:00": 3, "23:00": 10, "22:00": 7, "18:00": 1, "15:00": 2, "16:00": 1, "17:00": 1, "0:00": 8, "1:00": 1}, "Tuesday": {"19:00": 1, "17:00": 1, "1:00": 2, "21:00": 1, "23:00": 3}, "Monday": {"18:00": 2, "23:00": 1, "22:00": 2}}

第二

"hours": {"Monday": "11:00-21:00", "Tuesday": "11:00-21:00", "Friday": "11:00-22:00", "Wednesday": "11:00-21:00", "Thursday": "11:00-21:00", "Sunday": "11:00-21:00", "Saturday": "11:00-22:00"}}

和第三:

"yelping_since": "2006-01-18"

第三个不是很糟糕,我想我能够格式化它: 这是我使用的代码

import json
from datetime import datetime
datetime.strptime(data['yelping_since'],'%Y-%m-%d')

但是我和其他两个人有些麻烦。如果有人能给我一些指示,我将不胜感激。

如果您有任何疑问,请与我们联系

感谢您阅读

1 个答案:

答案 0 :(得分:1)

https://docs.python.org/2/library/json.html

前两个看起来像json - 试试上面的链接。它将为您提供如何使用它的想法。简而言之 - 不要自己解析json ......

例如在Postgres中使用json就像以下一样简单:

t=# select jsonb_object_keys(('{"time": {"Friday": {"20:00": 2, "19:00": 1, "22:00": 10, "21:00": 5, "23:00": 14, "0:00": 2, "18:00": 2}, "Thursday": {"23:00": 1, "0:00": 1, "19:00": 1, "18:00": 1, "16:00": 2, "22:00": 2}, "Wednesday": {"17:00": 2, "23:00": 3, "16:00": 1, "22:00": 1, "19:00": 1, "21:00": 1}, "Sunday": {"16:00": 2, "17:00": 2, "19:00": 1, "22:00": 4, "21:00": 4, "0:00": 3, "1:00": 2}, "Saturday": {"21:00": 4, "20:00": 3, "23:00": 10, "22:00": 7, "18:00": 1, "15:00": 2, "16:00": 1, "17:00": 1, "0:00": 8, "1:00": 1}, "Tuesday": {"19:00": 1, "17:00": 1, "1:00": 2, "21:00": 1, "23:00": 3}, "Monday": {"18:00": 2, "23:00": 1, "22:00": 2}}}'::jsonb)->'time'->'Sunday');
 jsonb_object_keys
-------------------
 0:00
 1:00
 16:00
 17:00
 19:00
 21:00
 22:00
(7 rows)

从第一个例子中获取“星期日”的键

,只需将其投放到time即可将::time添加到最后:

t=# select jsonb_object_keys(('{"time": {"Friday": {"20:00": 2, "19:00": 1, "22:00": 10, "21:00": 5, "23:00": 14, "0:00": 2, "18:00": 2}, "Thursday": {"23:00": 1, "0:00": 1, "19:00": 1, "18:00": 1, "16:00": 2, "22:00": 2}, "Wednesday": {"17:00": 2, "23:00": 3, "16:00": 1, "22:00": 1, "19:00": 1, "21:00": 1}, "Sunday": {"16:00": 2, "17:00": 2, "19:00": 1, "22:00": 4, "21:00": 4, "0:00": 3, "1:00": 2}, "Saturday": {"21:00": 4, "20:00": 3, "23:00": 10, "22:00": 7, "18:00": 1, "15:00": 2, "16:00": 1, "17:00": 1, "0:00": 8, "1:00": 1}, "Tuesday": {"19:00": 1, "17:00": 1, "1:00": 2, "21:00": 1, "23:00": 3}, "Monday": {"18:00": 2, "23:00": 1, "22:00": 2}}}'::jsonb)->'time'->'Sunday')::time;
 jsonb_object_keys
-------------------
 00:00:00
 01:00:00
 16:00:00
 17:00:00
 19:00:00
 21:00:00
 22:00:00
(7 rows)