Python Forum
Need help for extractring keys from dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help for extractring keys from dictionary
#1
Have this code:

if mail["Subject"].find("Alert for  Ta*** prod errors") > 0 :
          body = get_autosys_body(mail)
          info = {}
          segments = body.split(' ')
          for line in body.splitlines():
            if 'Application name' and 'null' in line:
                 info['test'] = segments[0] + ' ' + segments[1] + ' ' + segments[2]  + ' ' + segments[3] + ' ' + segments[4]
            elif 'Application name' in line:
                 info['test'] = segments[0] + ' ' + segments[1] + ' ' + segments[2]  + ' ' + segments[3] + ' ' + segments[4] + ' ' + segments[5] + segments[6] + ' ' + segments[7] +  ' ' + segments[8] + ' ' + segments[9]
          print (info)
which created info dictionary:

{'test': '\r\nApplication name: Ta***\r\nSource: eggs***\r\nTimestamp: 2019-***\r\nMessage:'}
{'test': '\r\nApplication name: Ta***\r\nSource: ***\r\nTimestamp: 2019-***\r\nMessage: HTTP"GET" "/api/fx/pr***/groups" responded 500'}
How to get Values after Application name, source and message (for second line example)

Ta***
HTTP"GET" "/api/fx/pr***/groups" responded 500'
Reply
#2
print(info.keys())
code in loop overwrites key 'test' on each iteration
Reply
#3
To get those values you can try:

# For second example where
# info={'test': '\r\nApplication name: Cl***\r\nSource: adc***\r\nTimestamp: 2019-***\r\nMessage: HTTP"GET" "/api/fx/pr***/groups" responded 500'}
data = dict(i.split(':',maxsplit=1) for i in info['test'].strip().split("\r\n"))
print(data['Application name'])
print(data['Source'])
print(data['Message'])
Reply
#4
Note that
if 'Application name' and 'null' in line:
may note behave as you expect
'Application name' is non-empty string so it is always considered True. That's why your condition is effectivery just
if 'null' in line:
If you want both 'Application name' and 'null' to be present in the line, then use
if 'Application name' in line and 'null' in line:
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding keys and values to a dictionary giladal 3 2,466 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  access dictionary with keys from another and write values to list redminote4dd 6 3,230 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Drop Keys From Dictionary donnertrud 8 3,674 May-30-2020, 11:39 AM
Last Post: DeaD_EyE
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,033 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 3,870 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck
  Retrieving dictionary keys within with another dictionay bazcurtis 8 2,806 Oct-29-2019, 10:06 PM
Last Post: bazcurtis
  json.dumps to keep dictionary keys batchenr 1 2,007 May-14-2019, 11:17 AM
Last Post: buran
  Reference new dictionary keys with a variable slouw 4 2,881 May-07-2019, 03:30 AM
Last Post: slouw
  Get specific key from multiple keys in python dictionary pradeepkumarbe 0 2,112 Mar-24-2019, 07:23 PM
Last Post: pradeepkumarbe
  Most efficient way to define sub keys of a dictionary? wrybread 1 2,092 Feb-21-2019, 12:23 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020