我試圖訪問Python中的JSON對象/字典然而得到的錯誤:串指數必須是整數
TypeError: string indices must be integers if script['title'] == "IT":
,這是我的代碼,試圖訪問該字典中特別關鍵:
def CreateScript(scriptsToGenerate):
start = time.clock()
apiLocation = ""
saveFile = ""
for script in scriptsToGenerate:
print script
if script['title'] == "IT":
time = script['timeInterval']
baseItem = ""
和scriptsToGenerate在使用此這使得HTTP請求我的API傳遞
def GetScripts(urlParameters):
result = requests.get(constURLString + "/" + str(urlParameters)).json()
return [x for x in result if x != []]
這裏是我打電話CreateScript
def RunInThread(ID):
startedProcesses = list()
Scripts = []
Scripts = GetScripts(ID)
scriptList = ThreadChunk(Scripts, 2)
for item in scriptList:
proc = Process(target=CreateScript, args=(item))
startedProcesses.append(proc)
proc.start()
#we must wait for the processes to finish before continuing
for process in startedProcesses:
process.join()
print "finished"
,我通過這個進CreateScript
這裏是我的腳本對象的輸出
{u'timeInterval': u'1800', u'title': u'IT', u'attribute' : 'hello'}
請顯示您用來調用'CreateScript'的* exact *代碼。 – poke
'腳本'不是你想象的那樣。口譯員不會說謊。 – TigerhawkT3
在調用'Process(target = CreateScript,args =(item))'之前打印您的項目並查看要調用的內容 – The6thSense