How to check if dict value contains word / line value?

I have a simple condition where I need to check if a dict value contains a value [Complted]in a specific key.

example :

'Events': [
                {
                    'Code': 'instance-reboot'|'system-reboot'|'system-maintenance'|'instance-retirement'|'instance-stop',
                    'Description': 'string',
                    'NotBefore': datetime(2015, 1, 1),
                    'NotAfter': datetime(2015, 1, 1)
                },
            ],

I need to check if the key contains Description [Complted]in it at startup. iee

'Descripton': '[Completed] Instance runs on degraded hardware

How can i do this? I'm looking for something like

if inst ['Events'][0]['Code'] == "instance-stop":
      if inst ['Events'][0]['Description'] consists   '[Completed]":
              print "Nothing to do here"
+4
source share
4 answers

That should work. You should use ininstead consists. In python, nothing is called consists.

"ab" in "abc"
#=> True

"abxyz" in "abcdf"
#=> False

So in your code:

if inst['Events'][0]['Code'] == "instance-stop":
      if '[Completed]' in inst['Events'][0]['Description']
          # the string [Completed] is present
          print "Nothing to do here"

Hope this helps :)

+1
source

,

   elif inst ['Events'][0]['Code'] == "instance-stop":
                        if "[Completed]" in inst['Events'][0]['Description']:
                            print "Nothing to do here"
+1

, 'Events' , .

Also, inst ['Events'][0]['Code'] == "instance-stop":it will not be true in the example you specified.

Try this as follows:

for key in inst['Events']:
    if 'instance-stop' in key['Code'] and '[Completed]' in key['Description']:
        # do something here
+1
source
for row in inst['Events']:
    if ( "instance-stop" in row['Code'].split('|')) and ((row['Descripton'].split(' '))[0] == '[Completed]'):
        print "dO what you want !"
0
source

All Articles