我想知道您是否可以用我目前正在處理的一段代碼來幫助我。我是Python新手,這是我嘗試編寫的第一個主要腳本之一。Python(DICT) - 使用JSON填充 - 不能在請求中使用變量
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]到請求中的方式一樣。
好像tagValues是不是在一些你從文件中讀取嵌套字典。介意提供輸入? –