我在 python 中有一个字典列表
,即
listofobs = [{'timestamp': datetime.datetime(2012, 7, 6, 12, 39, 52), 'ip': u'1.4.128.0', 'user': u'lovestone'}, {'timestamp': datetime.datetime(2012, 7, 6, 12, 40, 32), 'ip': u'192.168.21.45', 'user': u'b'}]
我想在 Django 模板中使用 listofobs
变量的所有键和值。例如:
对于第一次迭代:
timestamp = 7 july 2012, 12:39 Am
ip = 1.4.128.0
user = lovestone
第二次迭代:
timestamp = 7 july 2012, 12:40 Am
ip = 192.168.21.45
user = b
等等..
最佳答案
for a in listofobs:
print str( a['timestamp'] ), a['ip'], a['user']
将迭代字典列表,然后在模板中使用它们,只需将其包装在所需的 django 语法中,这与常规 python 非常相似。
关于python - 如何遍历python中的字典列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11370913/