字符串索引必须是整数 - Tableau JSON 集成

string indices must be integers - Tableau JSON Integration

我是 Python 的新手,正在尝试将其与 Tableau Extract API 连接器一起使用以形成 tables (.tde)。令人讨厌的是,在填充 table 行时尝试访问 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"}

你循环你的字典的方式不对

for item in data["data"]:

如果你这样循环,每次迭代item只会代表dict的key。例如:

>>> for item in {'a':1, 'b':2}:
...   print item
...
a
b

要获得所需的功能,您必须遍历字典的 .iteritems(),其中 returns 个(键,值)对元组。

for k, v in data["data"].iteritems():
  # k is now "Thresh"
  # v is now the dict that belongs to key "Thresh"

我想这仍然不是您所需要的,但下面的代码应该可以修复它

for champion, info_dict in data["data"].iteritems():
  for property_key, property_value in info_dict.iteritems():
    print property_value

输出:

Thresh
the Chain Warden
Mana
[{u'num': 0, u'id': 412000, u'name': u'default'}, {u'num': 1, u'id': 412001, u'name': u'Deep Terror Thresh'}, {u'num': 2, u'id': 412002, u'name': u'Championship Thresh'}, {u'num': 3, u'id': 412003, u'name': u'Blood Moon Thresh'}]
Thresh
412
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 ...

要获取每个皮肤的名称,请使用

for champion, info_dict in data["data"].iteritems():
  for property_key, property_value in info_dict.iteritems():
    if property_key == "skins":
        print [x["name"] for x in property_value]