1

I'm only a few days into my Python experience so I'm sorry to pollute the site with a baby-steps question. I'm trying to write an add-on for XBMC, which I believe uses Python 2.4. I've accessed a web api, which wraps the return in JSON. I've saved the url return to a WebHTML list (I think a list!). When I enter the following from the stackoverflow tutorial:

decoded = json.loads(WebHTML)
print 'DECODED:', decoded

I get the following in my log:

NOTICE: {u'totalItems': 1, u'query': u'Ludwig van Beethoven Op 67 Symphony No 5 in Cm (Fate)', u'kind': u'volumes#volumes', u'items': [{u'scoreId': u'IMSLP01056', u'pageCount': 42, u'title': u'Symphony No.5, Op.67 - Complete Score (S.464/5)', u'kind': u'volumes#volume', u'authors': [u'Beethoven, Ludwig van'], u'year': 1807, u'selfLink': u'http://www.peachnote.com/rest/api/v0/score?id=IMSLP01056'}]}

I want to extract the scoreId for a subsequent url call, i.e. IMSLP01056, but I'm struggling with the correct command. When I try:

dict = json.loads(WebHTML)
dict['items']['scoreId']
print dict['items']['scoreId']

The log error returned is:

TypeError: list indices must be integers, not str

If I try:

print dict[4][1]

it returns

KeyError: (4,)

I've tried a variety of JSONloads commands but can't get any to work. I always get an error beginning:

IOError: (2, 'No such file or directory', '{\n "kind": "volumes#volumes",\n "query": "Ludwig ..... etc, etc.

So, I'm a little stumped as to the correct command I should use. Thanks and sorry if this is too much info for a very basic question.

4
  • print dict['items'][0]['scoreId'] Commented Apr 30, 2013 at 9:51
  • 3
    Using dict as a variable name is a very bad idea as it shadows the built-in name. Commented Apr 30, 2013 at 9:52
  • Thanks larsmans, I've changed 'dict' to 'scoreId', though I still get the same error. Commented Apr 30, 2013 at 9:56
  • spicavigo - it still tells me must be integer not str. Commented Apr 30, 2013 at 10:00

1 Answer 1

2

The main response you're getting is indeed a dictionary, but the value for its items key is a list. So the error is telling you to use an integer index for that inner lookup. So:

dict['items'][0]['scoreId']

Note that there are potentially many items inside that items list, and the above will only give you the first: you probably want all of them in a single list:

scores = [item['scoreId'] for item in dict['items']]

which will iterate through all items, and extract their scores into a separate list.

Also, as larsmans says, don't use dict (or list) as a variable name, as it hides the built-in dict() function.

Sign up to request clarification or add additional context in comments.

1 Comment

Ooh, now it does work. Sorry, I forgot to amend the preceding line as well. Thanks everyone - told you I was a beginner!! Can't believe how quickly you helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.