Difficulty w/json keys

Rolando Espinoza La Fuente darkrho at gmail.com
Fri Apr 23 17:42:22 CEST 2010


On Fri, Apr 23, 2010 at 10:20 AM, Red <WanderingAengus at comcast.net> wrote:
[...]
> for line in f:
>        j = json.loads(line)
>        if 'text' in j:
>                if 'lang' in j:
>                        lang = j['lang']
>                        print "language", lang
>                text = j['text']

"lang" key is in "user" dict

>>> tweet['text']
'tech managers what size for your teams? better to have 10-20 ppl per
manager or 2-5 and have the managers be more hands on?'

>>> tweet['lang']
[...]
KeyError: 'lang'

>>> tweet['user']['lang']
'en'

~Rolando



More information about the Python-list mailing list