在运算符中输入变量时不起作用,而在分配值时起作用
In operator not working when the variable is inputted and works when the value is assigned
所以我有一个 yaml 文件。我试图从中提取价值。目前我不循环它并获得我想要的值,当这个问题解决时会这样做。但是当我输入我想使用的变量时它找不到密钥。否则它会起作用。
import yaml
f = open(path)
blueprints = yaml.load(f, Loader=yaml.FullLoader)
id = 682
if (id in blueprints) == True:
act = input("\ncopying\ninvention\nproducts\nskills\nmanufacturing\nActivity: ")
if (act in blueprints[id]["activities"] == True):
print(blueprints[id]["activities"][act])
else:
print("could not find the activity")
else:
print("could not find id")
所以输出是 "could not find the activity" 这意味着第一个 in 运算符有效,但第二个无效。我尝试通过输入第一个变量 "id" 而不是 "act" 来做完全相反的事情,但它也没有用。是的,我确实将 "id" 变量变成了整数。 yaml 文件太大,但这是其中的一部分:
681:
activities:
copying:
time: 480
manufacturing:
materials:
- quantity: 86
typeID: 38
products:
- quantity: 1
typeID: 165
time: 600
research_material:
time: 210
research_time:
time: 210
blueprintTypeID: 681
maxProductionLimit: 300
682:
activities:
copying:
time: 480
manufacturing:
materials:
- quantity: 133
typeID: 38
products:
- quantity: 1
typeID: 166
time: 600
research_material:
time: 210
research_time:
time: 210
blueprintTypeID: 682
maxProductionLimit: 300
我对 python 还是很陌生,在我自己学习的过程中没有人可以问。如果解决方案很简单,我很抱歉。
以下是所需的更改,请参阅评论。 Sample here:
import yaml
# f = open(path)
with open('data.yaml', 'r') as f: # change to use with context manager which will handle file closing
# Just change 'data.yaml', remember to indent the block.
blueprints = yaml.load(f, Loader=yaml.FullLoader)
id_no = 682 # change to use id_no
# id is a built in function you don't want to overwrite.
if id_no in blueprints: # Remove the superfluous == True
act = input("\ncopying\ninvention\nproducts\nskills\nmanufacturing\nActivity: ")
if act in blueprints[id_no]["activities"]: # Remove the erroneous == True, which causes your result to fail
print(blueprints[id_no]["activities"][act])
else:
print("could not find the activity")
else:
print("could not find id")
测试运行:
copying
invention
products
skills
manufacturing
Activity: copying
{'time': 480}
注意:请记住您的 input
(act
) 必须与 activities
中的键完全匹配,并且区分大小写。否则你会想要转换案例进行比较。
所以我有一个 yaml 文件。我试图从中提取价值。目前我不循环它并获得我想要的值,当这个问题解决时会这样做。但是当我输入我想使用的变量时它找不到密钥。否则它会起作用。
import yaml
f = open(path)
blueprints = yaml.load(f, Loader=yaml.FullLoader)
id = 682
if (id in blueprints) == True:
act = input("\ncopying\ninvention\nproducts\nskills\nmanufacturing\nActivity: ")
if (act in blueprints[id]["activities"] == True):
print(blueprints[id]["activities"][act])
else:
print("could not find the activity")
else:
print("could not find id")
所以输出是 "could not find the activity" 这意味着第一个 in 运算符有效,但第二个无效。我尝试通过输入第一个变量 "id" 而不是 "act" 来做完全相反的事情,但它也没有用。是的,我确实将 "id" 变量变成了整数。 yaml 文件太大,但这是其中的一部分:
681:
activities:
copying:
time: 480
manufacturing:
materials:
- quantity: 86
typeID: 38
products:
- quantity: 1
typeID: 165
time: 600
research_material:
time: 210
research_time:
time: 210
blueprintTypeID: 681
maxProductionLimit: 300
682:
activities:
copying:
time: 480
manufacturing:
materials:
- quantity: 133
typeID: 38
products:
- quantity: 1
typeID: 166
time: 600
research_material:
time: 210
research_time:
time: 210
blueprintTypeID: 682
maxProductionLimit: 300
我对 python 还是很陌生,在我自己学习的过程中没有人可以问。如果解决方案很简单,我很抱歉。
以下是所需的更改,请参阅评论。 Sample here:
import yaml
# f = open(path)
with open('data.yaml', 'r') as f: # change to use with context manager which will handle file closing
# Just change 'data.yaml', remember to indent the block.
blueprints = yaml.load(f, Loader=yaml.FullLoader)
id_no = 682 # change to use id_no
# id is a built in function you don't want to overwrite.
if id_no in blueprints: # Remove the superfluous == True
act = input("\ncopying\ninvention\nproducts\nskills\nmanufacturing\nActivity: ")
if act in blueprints[id_no]["activities"]: # Remove the erroneous == True, which causes your result to fail
print(blueprints[id_no]["activities"][act])
else:
print("could not find the activity")
else:
print("could not find id")
测试运行:
copying
invention
products
skills
manufacturing
Activity: copying
{'time': 480}
注意:请记住您的 input
(act
) 必须与 activities
中的键完全匹配,并且区分大小写。否则你会想要转换案例进行比较。