我对Python非常陌生,正在尝试将它与Tableau Extract API连接器结合使用,以形成表(.tde)。令人恼火的是,在填充表行时,当试图访问API中的数据时,我总是收到上面的错误消息。你们这些聪明人能帮我吗?你知道吗
import dataextract as tde
import os
import urllib2
import json
region = "euw"
urlPrefix = 'https://global.api.pvp.net'
apiLink = "/api/lol/static-data/" + region + "/v1.2/champion?champData=all&api_key="
apiKey = "my_api_key"
json_obj = urllib2.urlopen(urlPrefix + apiLink + apiKey)
#Step 0: Load the JSON Object
data = json.load(json_obj)
#Step1: Create the extract file
if os.path.isfile("Static" + "_" + "Champions" + "_" + "v1.2" + "_" + str.upper(region) + ".tde"):
os.remove("Static" + "_" + "Champions" + "_" + "v1.2" + "_" + str.upper(region) + ".tde")
tdefile = tde.Extract("Static" + "_" + "Champions" + "_" + "v1.2" + "_" + str.upper(region) + ".tde")
#Step2: Create table definition
tableDef = tde.TableDefinition()
tableDef.addColumn("Name", tde.Type.CHAR_STRING)
tableDef.addColumn("Title", tde.Type.CHAR_STRING)
tableDef.addColumn("Skins", tde.Type.CHAR_STRING)
tableDef.addColumn("Blurb", tde.Type.CHAR_STRING)
tableDef.addColumn("Resource Type", tde.Type.CHAR_STRING)
tableDef.addColumn("Primary Stats", tde.Type.DOUBLE)
tableDef.addColumn("Secondary Stats", tde.Type.DOUBLE)
#Step3: Create the table in the image of the table definition
table = tdefile.addTable("Extract", tableDef)
#Step4: Populate the table with data and create rows
newRow = tde.Row(tableDef)
for item in data["data"]:
newRow.setCharString(0, item["name"])
newRow.setCharString(1, item["title"])
newRow.setCharString(2, item["skins"])
newRow.setCharString(3, item["blurb"])
newRow.setCharString(4, item["partype"])
newRow.setDouble(5, item["info"])
newRow.setDouble(6, item["stats"])
table.insert(newRow)
#Step 5: CLose the TDE
tdefile.close()
错误消息是:
Traceback (most recent call last):
File "C:\Users\Stef\workspace\Tableau_Extract_API\Tableau_Extract_API\static_api_champions.py", line 42, in <module>
newRow.setCharString(0, item["name"])
TypeError: string indices must be integers
示例数据:
{"type":"champion","version":"5.7.2","data":{"Thresh":{"id":412,"key":"Thresh","name":"Thresh","title":"the Chain Warden","skins":[{"id":412000,"name":"default","num":0},{"id":412001,"name":"Deep Terror Thresh","num":1},{"id":412002,"name":"Championship Thresh","num":2},{"id":412003,"name":"Blood Moon Thresh","num":3}],"blurb":"Thresh is a sadistic, spectral reaper who relishes tormenting the living and the dead. Once a jailer who mercilessly brutalized all under his charge, Thresh was hanged from his own chains by the prisoners he had tortured. With his vicious essence ...","partype":"Mana"}
你绕着听写的方式不对
如果这样循环,则每个迭代
item
将只表示dict的键。例如:要获得所需的功能,必须循环dict的
.iteritems()
,它返回(键,值)对的元组。你知道吗我想这仍然不是您所需要的,但下面的代码应该可以修复它
输出:
要获取每个皮肤的名称,请使用
相关问题 更多 >
编程相关推荐