tags:

views:

517

answers:

5

Hi all,

I am very new to Python and parsing data.

I can pull an external JSON feed into Python and iterate over the results.

for r in results:
     print r['key_name']

As I walk through the results returned, I am getting an error when a key does not have a value (a value may not always exist for a record). If I print the results, it shows as

'key_name': None, 'next_key':.................

My code breaks on the error. How can I control for a key not having a value?

Any help will be greatly appreciated!

Brock

+4  A: 

Either use the in operator to detect presence of the key, or catch the KeyError exception that results.

Ignacio Vazquez-Abrams
Sorry, I am really new to Python. How would I do that? Many thanks for your time
Btibert3
http://docs.python.org/tutorial/datastructures.html#dictionaries http://docs.python.org/tutorial/errors.html
Ignacio Vazquez-Abrams
A: 

use has_key() , and that will return true or false

TIMEX
`d.has_key(k)` is deprecated now. You're supposed to use `k in d` in new code.
LeafStorm
+7  A: 

The preferred way, when applicable:

for r in results:
     print r.get('key_name')

this will simply print None if key_name is not a key in the dictionary. You can also have a different default value, just pass it as the second argument:

for r in results:
     print r.get('key_name', 'Missing: key_name')

If you want to do something different than using a default value (say, skip the printing completely when the key is absent), then you need a bit more structure, i.e., either:

for r in results:
    if 'key_name' in r:
        print r['key_name']

or

for r in results:
    try: print r['key_name']
    except KeyError: pass

the second one can be faster (if it's reasonably rare than a key is missing), but the first one appears to be more natural for many people.

Alex Martelli
A: 

[Updated to remove careless mistake]

You could also do something like this:

for r in (row for row in results if 'a' in row):
    print r['a']

This uses a generator expression to pick "rows" out of "results" where "row" includes the key "a".

Here's a little test script:

results = [{'a':True}, {'b':True}, {'a':True}]
for r in (row for row in results if 'a' in row): print r['a']
Parand
`[r.get('key_name') for r in results]`
Adam Bernier
@Parand: What version of Python did you test that on? Both your `for` statments draw a **SYNTAX** **ERROR** in Python 2.6 and 3.1.
John Machin
Sorry John, that was a careless mistake. Fixed the script, should now work on python 2.5+ .
Parand
+1  A: 

If possible, use the simplejson library for managing JSON data.

Daishiman