我有一个字典self.ticketDict1
,看起来像这样。
2457 : {'origin': u'HW',
'department': u'Kunde',
'ticket-closed': True,
'prio-events': [(datetime.datetime(2015, 4, 27, 16, 53, 55, 64183, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'Wichtig')],
'status-events': [(datetime.datetime(2015, 4, 27, 16, 53, 55, 64183, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'new'),
(datetime.datetime(2015, 5, 15, 16, 5, 0, 179351, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'closed'),
(datetime.datetime(2015, 5, 28, 13, 32, 47, 523975, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'closed'),
(datetime.datetime(2015, 6, 9, 16, 8, 16, 726823, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'closed')]}
2458 : {'origin': u'HW',
'department': u'Kunde',
'ticket-closed': True,
'prio-events': [(datetime.datetime(2015, 4, 27, 16, 53, 55, 77779, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'Wichtig')],
'status-events': [(datetime.datetime(2015, 4, 27, 16, 53, 55, 77779, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'new'),
(datetime.datetime(2015, 5, 15, 15, 12, 53, 29570, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'closed')]}
2459 : {'origin': u'HW',
'department': u'Kunde',
'ticket-closed': True,
'prio-events': [(datetime.datetime(2015, 4, 27, 16, 53, 55, 94126, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'W\xfcnschenswert')],
'status-events': [(datetime.datetime(2015, 4, 27, 16, 53, 55, 94126, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'new'),
(datetime.datetime(2015, 5, 11, 14, 47, 28, 916677, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'closed')]}
现在我想从每个项目的第一个status-events
值打印所有日历周。
我试过这种方式:
for i in self.ticketDict1:
print i['status-events'][0][0].isocalendar()[1]
但我总是得到这个错误:
File "/media/sf_Projects/Ticketauswertung/Converting.py", line 86, in ConvertData
print i['status-events'][0][0].isocalendar()[1]
TypeError: 'int' object has no attribute '__getitem__'
编辑: 如果我以这种方式打印我的字典:
for i in self.ticketDict1:
print i, ' : ', self.ticketDict1[i]
print '\n'
我得到了这个结果:
2556 : {'origin': u'HW', 'department': u'Intern', 'ticket-closed': False, 'prio-events': [(datetime.datetime(2015, 7, 27, 16, 30, 59, 547747, tzinfo=<LocalTimezone "CEST" 2:00:00>), u'Wichtig')], 'status-events': [(datetime.datetime(2015, 7, 27, 16, 30, 59, 547747, tzinfo=<LocalTimezone "CEST" 2:00:00>), u'new')]}
2557 : {'origin': u'HW', 'department': u'Intern', 'ticket-closed': False, 'prio-events': [(datetime.datetime(2015, 7, 27, 16, 32, 37, 491657, tzinfo=<LocalTimezone "CEST" 2:00:00>), u'Wichtig')], 'status-events': [(datetime.datetime(2015, 7, 27, 16, 32, 37, 491657, tzinfo=<LocalTimezone "CEST" 2:00:00>), u'new')]}
2558 : {'origin': u'HW', 'department': u'Intern', 'ticket-closed': False, 'prio-events': [(datetime.datetime(2015, 7, 27, 16, 33, 51, 29451, tzinfo=<LocalTimezone "CEST" 2:00:00>), u'Wichtig')], 'status-events': [(datetime.datetime(2015, 7, 27, 16, 33, 51, 29451, tzinfo=<LocalTimezone "CEST" 2:00:00>), u'new')]}
正如您所看到的,我的字典没有任何问题。
答案 0 :(得分:1)
以下对我来说很好:
>>> x = {'status-events': [(datetime.datetime(2015, 4, 27, 16, 53, 55, 64183, None), u'new'),
(datetime.datetime(2015, 5, 15, 16, 5, 0, 179351, None), u'closed'),
(datetime.datetime(2015, 5, 28, 13, 32, 47, 523975, None), u'closed'),
(datetime.datetime(2015, 6, 9, 16, 8, 16, 726823, None), u'closed')]}
>>> x['status-events'][0][0].isocalendar()
(2015, 18, 1)
>>> x['status-events'][0][0].isocalendar()[1]
18
这是因为你循环遍历self.TicketDict1,它将为你提供字典中的每个键。你想要的是这个:
for i in self.TicketDict1['statusevents'].values():
i[0][0].isocalendar()[1]
我不确定2457
是什么(我假设冒号丢失了,应该说2457:
),所以你可能会想要这样:
for i in self.TicketDict1[2457]['statusevents'].values():
i[0][0].isocalendar()[1]
您还希望使用dictionary.values()来获取值,而不是键。例如:
x = {1:'one', 2:'two'}
for i in x.values():
print i
>>> one
>>> two
,同时:
x = {1:'one', 2:'two'}
for i in x:
print i
>>> 1
>>> 2
当您循环代码当前的编写方式时,您将获得密钥。该错误可能来自尝试从字典中的一个键中检索项目,例如。 2457['status-events'][0][0].isocalendar()[1]
会抛出错误。
注意:
x = {1:'one', 2:'two'}
for i in x:
print i[1]
>>> Traceback (most recent call last):
File "python", line 4, in <module>
TypeError: 'int' object has no attribute '__getitem__'
答案 1 :(得分:0)
好的,我自己解决了。不过感谢快速提问!
代码:
for i in self.ticketDict1:
for j,k in self.ticketDict1[i]['status-events']:
print i, ' : ', j.isocalendar()[1]
break
结果:
1459 : 5
1977 : 16
1978 : 16
1979 : 16
2498 : 29
3011 : 44
2500 : 29
3013 : 45