字符串索引必须是整数Tableau JSON Integration

2024-05-19 10:28:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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"}

Tags: thenameapidatatypetableextractitem
1条回答
网友
1楼 · 发布于 2024-05-19 10:28:36

你绕着听写的方式不对

for item in data["data"]:

如果这样循环,则每个迭代item将只表示dict的。例如:

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

要获得所需的功能,必须循环dict的.iteritems(),它返回(键,值)对的元组。你知道吗

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]

相关问题 更多 >

    热门问题