我有一个词典提醒[],其中包括关键的alert_date.所有日期/时间都存储为字符串.我在Django中显示日期,但模板无法格式化存储为sting的时间.所以,我想在我的视图中将其转换为:
foo = "2014-10-07 00:00:00"
bar = datetime.strptime(foo, "%Y-%m-%d %H:%M:%S")
但是,我想立刻转换所有字典时间值.我通过API调用将字典作为JSON抓取.我想做这样的事情(这显然是无效的代码):
alerts = resp_data['alerts']
for v in alerts['alert_date']:
v = datetime.strptime(v, "%Y-%m-%d %H:%M:%S")
//编辑:
JSON响应是一个包含警报的字典,它是一个字典列表,如下所示:
{"alerts": [
{
"key1": "value11",
"key2": "value12",
"key3": "value13",
"alert_date": "2014-06-05 01:00:23.633000",
},
{
"key1": "value21",
"key2": "value22",
"key3": "value23",
"alert_date": "2010-12-31 00:00:00",
}
]}