Python Json搜索函数

Python Json搜索函数,python,json,Python,Json,我正在尝试创建一个搜索函数,其中我获取用户的输入,然后它通过我的config.json进行搜索,以查看是否有同名条目,如果有,则打印值,如果没有,则传递 recipient_finder = input("Recipient: ") for receiver in config['email_list']: if receiver == recipient_finder: print([recipient_finder]) 这在我的config.json中 "ema

我正在尝试创建一个搜索函数,其中我获取用户的
输入
,然后它通过我的
config.json
进行搜索,以查看是否有同名条目,如果有,则打印值,如果没有,则
传递

recipient_finder = input("Recipient: ")
for receiver in config['email_list']:
    if receiver == recipient_finder:
        print([recipient_finder])
这在我的
config.json中

  "email_list": {
    "mediamarkt": "contact@mediamarkt.nl",
    "lenovo": "consumerts@lenovo.com",
    "godaddy": "hq@godaddy.com",
    "stackoverflow": "legal@stackoverflow.com"
    }

问题是,我并没有从搜索中得到一封真正的电子邮件,而是一个密钥(所以我不会得到)hq@godaddy.com但是只要godaddy)

您可以尝试以下方法:

recipient_finder = input("Recipient: ")
for receiver in config['email_list']:
    if receiver == recipient_finder:
        print(config['email_list'][recipient_finder])


检查用户输入是否为字典中的键。如果是,则打印相应的值

if receiver in config['email_list']:
    print(config['email_list'][receiver]
else:
    print("Not found")

这里有什么问题?您是在字典中搜索键还是值
receiver
在按键上循环。不需要循环,只要
if receiver in config['email_list']:
我需要的是程序查看字符串,然后给出值,因此如果用户键入
lenovo
,它应该打印
consumerts@lenovo.com
?这起作用了,我还不能接受你的回答,但会尽快回答。
if receiver in config['email_list']:
    print(config['email_list'][receiver]
else:
    print("Not found")