重新搜索字典键并返回值

时间:2021-04-22 19:18:54

标签: python re

python 中的新用户。

我正在尝试通过检查字典使 MX 记录可读。

这只返回“else”,但我不知道为什么。

import re

mx_dict = {
    "l.google.com": "Google",
    "protection.outlook.com": "Outlook",
    "one.com": "One"
}

def mx_check(mx_record):
    for key, value in mx_dict.items():
        if re.search(key, mx_record):
            return value
        else:
            return mx_record

print(mx_check('aspmx.l.google.com.'))

找到解决方案,谢谢@khelwood

import re
mx_class = {
    "l.google.com": "Google",
    "one.com": "One",
    "domenehost.no": "Domenehost"
}

def mx_check(mx_record):
    for key, value in mx_class.items():
        if re.search(key, mx_record):
            mx_record = value
    
    return mx_record

print(mx_check('mx2.pub.mailpod3-cph3.one.com.'))

0 个答案:

没有答案