Я очень новичок в Python и пытаюсь использовать его с помощью соединителя API Tableau Extract для формирования таблиц (.tde). Раздражающе продолжаю получать сообщение об ошибке выше при попытке доступа к данным в API при заполнении строк таблицы. Может ли кто-нибудь из вас умных людей помочь мне?Строковые индексы должны быть целыми числами - Tableau JSON Integration
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"}
Как выглядят данные dict? –
Просто добавил пример набора данных из первой записи - никогда бы не догадался, что я игрок в лиге ;-) Приветствия! – StefA
Ошибка журнала не соответствует вашему коду –