我有一本字典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')]}
如你所见,我的字典没有任何问题。
最佳答案
以下内容对我来说效果很好:
>>> 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
当您按照当前编写的方式循环代码时,您将获得 key 。该错误可能是由于尝试从字典中的某个键检索项目,例如。 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__'
关于Python:如何从字典中打印某些值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33889430/