简体   繁体   中英

How do I compare a string to a unicode dictionary?

Okay, so what I'm attempting to do is whenever a server at work goes down, I get an email informing me that it went down, along with some other information. What I'm trying to do is write something to pull the criticality of the server that went down (which changes often) and take a specific action depending on its criticality. So what I did was took an excel document with all that information, and created a dictionary with it. The keys are all server names, and the values are their criticality. I then try and compare the server name from the email to the dictionary to pull the criticality of the server. Here's a few print statements along with their results.

print server_name
print server_list
print server_name in [server_list]

I get:

ABC123
{'': '', u'ABC123': u'High':''}
False

I am still very new to python, but I assumed I got False when I checked if server_name was in server_list because I'm trying to compare a string to unicode. So I tried to convert the string to unicode, and it still didn't work (tried to verify by using type(server_name) to make sure they were both the same type).

Now here's a larger chunck of my 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 is found in the server list'

    elif server_name not in [server_list]:
        print 'server name is not found in the server list'
        criticality = server_list[server_name]
        print server_name
        print criticality

Which gives me the output:

ABC123
High

So when I run this section, because my 'if' statement is False, it runs my elif statement which is True. It then prints out the server name properly, as well as the criticality. What I don't understand is why I can't find the key in the dictionary, but I can pull the value of the key that I can't find? Also, if If i change my 'if' statement from

if server_name in [server_list]

to

if server_name not in [server_list]

I can get it to function exactly the way I want it to, but it really bothers me that I don't understand why it works that way.

This has nothing to do with unicode whatsoever.

  1. Your server_list is actually a dictionary.
  2. When you say server_name in [server_list] you're putting your dictionary in a list (ie it becomes [{'': '', u'ABC123': u'High':''}] ), and then seeing if server_name is in that list. Which it isn't, since it's a string.

You just need to change your test to: if server_name in server_list without the surrounding brackets.
Also, better to rename it to server_dict .

Just to illustrate:

my_dict = {'ABC': True}
'ABC' in [my_dict]  # False
'ABC' in my_dict  # True

I assume your problem is here:

if server_name in [server_list]:

In case if server_list has type list, you should use it as follows:

if server_name in server_list:

Check this example:

>>> lst = ['this', 'is', 'example']
>>> 'is' in lst
True
>>> 'is' in [lst]
False

Upd.: Also, take into account, for your own safety, convert all string values to lowercase, because 'is' and 'Is' are different strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM