在 Python 中打印字典中特定键的值

Print the value of specific key from Dictionary in Python

我有Input.txt,这个文件的内容是:

Name1=Value1
Name2=Value2
Name3=Value3

期望的输出:得到key==Name1的值。

条件:需要Python中的Dictionary实现。

with open("Input.txt", "r") as param_file:
    text = param_file.readlines()
    d = dict(x.strip().split("=") for x in text)
    for k, v in d.items():
        if k == "Name1":
            print(f"{d[k]}")

你可以做到

with open("Input.txt", "r") as param_file:
    text = param_file.readlines()
    dc = {y.split("=")[0]:y.split("=")[1] for y in text}
    print(dc["Name1"]) if "Name1" in dc else None

那会输出

Value1