k.troy2012
k.troy2012

Reputation: 344

Python: How to print certain values from a dictionary

I got a dictionary self.ticketDict1 which looks like this.

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')]}

Now I want to print all calendarweeks from the very first status-events value of each item.

I've tried it this way:

for i in self.ticketDict1:
      print i['status-events'][0][0].isocalendar()[1]

But I always get this Error:

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__'

EDIT: If I print my dictionary this way:

for i in self.ticketDict1:
            print i, ' : ', self.ticketDict1[i]
            print '\n'

I get this result:

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')]}

As you can see there is nothing wrong with my dictionary.

Upvotes: 1

Views: 132

Answers (2)

k.troy2012
k.troy2012

Reputation: 344

Ok, I've solved it by myself. Nevertheless thanks for the quick questions!

Code:

for i in self.ticketDict1:
        for j,k in self.ticketDict1[i]['status-events']:
            print i, ' : ', j.isocalendar()[1]
            break

Result:

1459  :  5
1977  :  16
1978  :  16
1979  :  16
2498  :  29
3011  :  44
2500  :  29
3013  :  45

Upvotes: 0

NotAnAmbiTurner
NotAnAmbiTurner

Reputation: 2743

The following works fine for me:

>>> 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

It's because you are looping through self.TicketDict1, which is going to give you each key in the dictionary. What you want is this:

for i in self.TicketDict1['statusevents'].values():
    i[0][0].isocalendar()[1]

I'm not sure what 2457 is (I'm assuming a colon is missing, and it should say 2457:), so you might want this instead:

for i in self.TicketDict1[2457]['statusevents'].values():
    i[0][0].isocalendar()[1]

You also want to use dictionary.values() to get the values, as opposed to the keys. For example:

x = {1:'one', 2:'two'}

for i in x.values():
    print i

>>> one
>>> two

while:

x = {1:'one', 2:'two'}

for i in x:
    print i

>>> 1
>>> 2

As you loop your code how it is currently written, you are getting the keys. The error is probably coming from an attempt to retrieve items from one of the keys in your dictionary, eg. 2457['status-events'][0][0].isocalendar()[1] will throw an error.

Note:

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__'

Upvotes: 1

Related Questions