Reputation: 3
I am very new to Python and am trying to use it with the Tableau Extract API connector to form tables (.tde). Annoyingly I keep getting the error message above when trying to access the data in the API when populating the table rows. Can any of you smart people help me?
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()
Error Message is:
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
Example Data:
{"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"}
Upvotes: 0
Views: 182
Reputation: 43354
The way you loop over your dict is not right
for item in data["data"]:
If you loop like this, each iteration item
will only represent the key of the dict. For example:
>>> for item in {'a':1, 'b':2}:
... print item
...
a
b
To get the required functionality, you must loop over the dict's .iteritems()
, which returns tuples of (key, value) pairs.
for k, v in data["data"].iteritems():
# k is now "Thresh"
# v is now the dict that belongs to key "Thresh"
This is still not exactly what you need I suppose, but below code should fix it
for champion, info_dict in data["data"].iteritems():
for property_key, property_value in info_dict.iteritems():
print property_value
outputs:
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 ...
To get the name for each of the skins, use
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]
Upvotes: 2