我想知道你是否可以帮助我处理我目前正在处理的一段代码。 我是Python的新手,这是我尝试编写的第一个主要脚本之一。
import json, sys from pprint import pprint #Importing workbench json output into the python script. with open('jsonoutput.json') as data_file: data = json.load(data_file) #Sets the verible for the while loop. x = int(0) while x <= 1: y = x print type(data) jdata = data["result"]["items"][y]["tagValues"]["IdDevicesMap"]["value"] if setup_1(jdata) == True: Default_1 += 1 else: print "exiting"运行时出错:
Traceback (most recent call last): File "main.py", line 47, in <module> jdata = data["result"]["items"][y]["tagValues"]["IdDevicesMap"]["value"] KeyError: 'tagValues'同样在侧面注释,当我手动输入列表编号[y]为1时,代码运行完美。 所以它就像我将变量[y]输入到请求中一样存在问题。
I was wondering if you could help me with a piece of code i'm working on at the moment. I'm new to Python and this is one of the first major scripts i have tried to write.
import json, sys from pprint import pprint #Importing workbench json output into the python script. with open('jsonoutput.json') as data_file: data = json.load(data_file) #Sets the verible for the while loop. x = int(0) while x <= 1: y = x print type(data) jdata = data["result"]["items"][y]["tagValues"]["IdDevicesMap"]["value"] if setup_1(jdata) == True: Default_1 += 1 else: print "exiting"Error i get when it runs:
Traceback (most recent call last): File "main.py", line 47, in <module> jdata = data["result"]["items"][y]["tagValues"]["IdDevicesMap"]["value"] KeyError: 'tagValues'Just on a side note as well, when i manually put in the list number [y] to 1 the code runs perfectly. So it's like its got an issue with the way I'm inputting the variable [y] into the request.
最满意答案
我很确定你读过的json在每一个中都没有tagValues。 您可能想尝试try : except:
import json, sys from pprint import pprint #Importing workbench json output into the python script. with open('jsonoutput.json') as data_file: data = json.load(data_file) x = 0 while True: try: jdata = data["result"]["items"][x]["tagValues"]["IdDevicesMap"]["value"] if setup_1(jdata) == True: Default_1 += 1 else: print "exiting" break except KeyError: print data["result"]["items"][x] pass x+=1要以pythonic的方式做到这一点:
import json, sys from pprint import pprint #Importing workbench json output into the python script. with open('jsonoutput.json') as data_file: data = json.load(data_file) for x, d in enumerate(data["result"]["items"]): #in case you need a counter try: jdata = d["tagValues"]["IdDevicesMap"]["value"] if setup_1(jdata) == True: Default_1 += 1 else: print "exiting" break except KeyError: passI am pretty sure the json you read doesn't have tagValues in every one of them. You might wanna try try: and except:
import json, sys from pprint import pprint #Importing workbench json output into the python script. with open('jsonoutput.json') as data_file: data = json.load(data_file) x = 0 while True: try: jdata = data["result"]["items"][x]["tagValues"]["IdDevicesMap"]["value"] if setup_1(jdata) == True: Default_1 += 1 else: print "exiting" break except KeyError: print data["result"]["items"][x] pass x+=1To do it in a pythonic way :
import json, sys from pprint import pprint #Importing workbench json output into the python script. with open('jsonoutput.json') as data_file: data = json.load(data_file) for x, d in enumerate(data["result"]["items"]): #in case you need a counter try: jdata = d["tagValues"]["IdDevicesMap"]["value"] if setup_1(jdata) == True: Default_1 += 1 else: print "exiting" break except KeyError: pass更多推荐
发布评论