簡體   English   中英

Python:如何從字典中打印某些值

[英]Python: How to print certain values from a dictionary

我有一個字典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

當您循環編寫當前代碼的方式時,就會得到密鑰。 該錯誤可能是由於嘗試從字典中的鍵之一中檢索項目而引起的。 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__'

好的,我自己解決了。 不過,感謝您的快速提問!

碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM