python - How do I compare a string to a unicode dictionary? -


okay, i'm attempting whenever server @ work goes down, email informing me went down, along other information. i'm trying write pull criticality of server went down (which changes often) , take specific action depending on criticality. did took excel document information, , created dictionary it. keys server names, , values criticality. try , compare server name email dictionary pull criticality of server. here's few print statements along results.

print server_name print server_list print server_name in [server_list] 

i get:

abc123 {'': '', u'abc123': u'high':''} false 

i still new python, assumed got false when checked if server_name in server_list because i'm trying compare string unicode. tried convert string unicode, , still didn't work (tried verify using type(server_name) make sure both same type).

now here's larger chunck of code:

    email_subject = str(messages.getlast())     period = email_subject.find('.')     period = int(period)     server_name = email_subject[0:period]     server_name = str(server_name.upper())     if server_name in [server_list]:         print 'server name found in server list'      elif server_name not in [server_list]:         print 'server name not found in server list'         criticality = server_list[server_name]         print server_name         print criticality 

which gives me output:

abc123 high 

so when run section, because 'if' statement false, runs elif statement true. prints out server name properly, criticality. don't understand why can't find key in dictionary, can pull value of key can't find? also, if if change 'if' statement from

if server_name in [server_list] 

to

if server_name not in [server_list] 

i can function way want to, bothers me don't understand why works way.

this has nothing unicode whatsoever.

  1. your server_list dictionary.
  2. when server_name in [server_list] you're putting dictionary in list (i.e. becomes [{'': '', u'abc123': u'high':''}]), , seeing if server_name in list. isn't, since it's string.

you need change test to: if server_name in server_list without surrounding brackets.
also, better rename server_dict.

just illustrate:

my_dict = {'abc': true} 'abc' in [my_dict]  # false 'abc' in my_dict  # true 

Comments