
[英]How to print one specific value from each key in a dictionary in Python?
[英]Print the value of specific key from Dictionary in Python
我有Input.txt
,这个文件的内容是:
Name1=Value1
Name2=Value2
Name3=Value3
所需的 output:获取key==Name1
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
那将 output
Value1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.