[Here is the API snippet. I am working on]
Here is the code snippet that fetching API snippet above.
JavaScript
x
8
1
for i in parse_json["lines"]:
2
list = []
3
if (i["chaosValue"] > chaos_ex_ratio):
4
value = (i["variant"],i["baseType"], i["exaltedValue"], "Exalted Orb", i["levelRequired"])
5
else:
6
value = (i["variant"],i["baseType"], i["exaltedValue"], "Chaos Orb", i["levelRequired"])
7
list.append(value)
8
If I remove i[“variant”] everything works perfectly but i[“variant”] is throwing key error even tho “variant”: “Shaper/Redeemer” exists on the JSON file. Thanks for any help.
Error code:
JavaScript
1
7
1
Traceback (most recent call last):
2
File "C:UsersemoscPycharmProjectsGithubPushspsychescape_price_fetcherpsychescape_price_fetchermain.py", line 265, in <module>
3
BaseType_Values()
4
File "C:UsersemoscPycharmProjectsGithubPushspsychescape_price_fetcherpsychescape_price_fetchermain.py", line 219, in BaseType_Values
5
value = (i["variant"],i["baseType"], i["exaltedValue"], "Exalted Orb", i["levelRequired"])
6
KeyError: 'variant'
7
Advertisement
Answer
You’re showing us a screenshot where the first line has a variant
, but your code will fail unless every line has a variant.
What is the output of this snippet?
JavaScript
1
4
1
for l in parse_json["lines"]:
2
if "variant" not in l:
3
print("Line {} does not have a 'variant'".format(l))
4
If it outputs any lines, there’s your problem. If it doesn’t, something deeper is going on. I would try to catch the KeyError
exception and put a breakpoint in the handler to continue investigating.