tags:

views:

71

answers:

5

my apologies in advance for a python and json nubie question. json seems to be hiccuping on the following statements:

{"delete":{"status":{"id":12600579001,"user_id":55389449}}}

code snippet:

temp = json.loads(line)
text = temp['text']

I get the following error output when the above code snippet encounters lines similar to the above json 'dictionary':

text = temp['text']
KeyError: 'text'

Is it because there is no "text" key in the line or because "delete" is not in the dictionary?

A: 

Why not put this between the first and second lines:

print temp
SamB
A: 

It looks like this happens because 'text' is not in there. Maybe you could use something like

'text' in temp

to check that 'text' exists before trying to use it.

Edit:

I've taken the example given in the comment and added a if/elif/else block to it.

#! /usr/bin/python
import sys
import json
f = open(sys.argv[1])
for line in f:
    j = json.loads(line)
    try:
        if 'text' in j:
            print 'TEXT: ', j['text']
        elif 'delete' in j:
            print 'DELETE: ', j['delete']
        else:
            print 'Everything: ', j
    except: 
        print "EXCEPTION: ", j

Sample Chunk #1:

{u'favorited': False, u'contributors': None, u'truncated': False, u'text': ---- snip ---- }

Sample Chunk #2:

{u'delete': {u'status': {u'user_id': 55389449, u'id': 12600579001L}}}

ChronoPositron
I thought that was the problem and jinned up the code below:#! /usr/bin/pythonimport sysimport jsonf = open(sys.argv[1])for line in f: j = json.loads(line) try: 'text' in j print "TEXT: ", j except: print "EXCEPTION: ", j continueand get the following results (only two sample chunks)... TEXT: {u'favorited': False, u'contributors': None, u'truncated': False, u'text': ---- snip ---- }TEXT: {u'delete': {u'status': {u'user_id': 55389449, u'id': 12600579001L}}}
I added a modified version of your code to my answer. Does this work for your file?
ChronoPositron
This got me past the first hurdle. Thanks!
A: 

From the snippet you posted, it looks like temp should only have one item, with the key "delete". You do not have a key 'text', so I am not sure what temp['text'] should look up.

Mike Graham
A: 

Is it because there is no "text" key in the line or because "delete" is not in the dictionary?

It's because there is no "text" key. If you print temp or check whether the key 'text' is in the resulting Python dictionary, you'll notice that there is no key named 'text'. In fact, temp only has one key: 'delete'. The dictionary that is referenced by 'delete' contains a single key 'status' that contains another dictionary with two keys: 'user_id' and 'id'.

In other words, your structure is this:

{
    "delete" : {
        "status" : {
            "id" : 12600579001,
            "user_id" : 55389449
        }
    }
}

As you can see, there is no "text" key anywhere.

Furthermore, you can check it yourself:

>>> 'text' in temp
False
>>> 'delete' in temp
True
Dustin
A: 

Thanks to all for the suggestions. The heart of the problem was that the Twitter json format has a dictionary within a dictionary. The solution involves a double index to get at the variables that I need to check.