0

I'm working w/the twitter search API and using python to parse the data coming back. I'm a python-noob, so my error is probably my lack of understanding, though I'm stuck.

My response looks like this:

And my code; "responseJson" is a file that I've saved the response from twitter to for continued testing w/out hitting the API.

responseJson = json.loads(open(DEBUG_JSON).read())

for key, value in responseJson.iteritems():
if key == 'results':
    print "type: ", type(value)
    for tweetKey, tweetValue in value.iteritems():
        print tweetKey

When I execute the program, I get:

$ python search.py
type:  <type 'list'> <-- says it's a list!
Traceback (most recent call last):
File "search.py", line 46, in <module>
for tweetKey, tweetValue in value.iteritems():
AttributeError: 'list' object has no attribute 'items'

You can see the output says "value" is a list, which means I should be able to call iteritems() on it. I'm albe to call iteritems() on "responseJson just fine.

Any help / clarity is appreciated, TIA!

1 Answer 1

1

iteritems() can only be run on dicts and similar objects. JSON, when loaded, is a dict. In this case, responseJson is a dict, not a list.

Lists have no such method; you can iterate over them by default. You may want to doublecheck what value actually is. If it's a 2-element list you can just remove the iteritems(). Otherwise you'll have to investigate the structure more carefully.

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

3 Comments

type(value) seems to think it's a list - I will investigate further.
It may help to do import pprint; pprint.pprint(responseJson). You'll be able to see exactly what you're working with.
Thanks - I got this working: results = responseJson['results'] for tweet in results:

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.