Python(DICT)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
Python(DICT) - 使用JSON填充 - 无法在请求中使用变量(Python (DICT) - Populated with JSON - Can't use variable in request)

我想知道你是否可以帮助我处理我目前正在处理的一段代码。 我是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: pass

I 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+=1

To 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

更多推荐

本文发布于:2023-04-17 08:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/bba030b32a925f1c53c20d76b79614bc.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   DICT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!