json - Python Dictionary key index issue (possibly scope?) -


i've run strange issue when pulling text json response querying twitter api.

if pull text in manner:

def retrievetext(raw_tweets):     l in raw_tweets:        format_data = json.loads(l)          k in format_data.keys():            if k == 'text':                text = format_data['text'] 

i not have issues. however, bit cumbersome iterate on list of keys.

if change version:

def retrievetext(raw_tweets):     l in raw_tweets:         format_data = json.loads(l)         text = format_data['text'] 

i receive keyerror, not make sense me based on results of previous code snippet.

if has insight on overlooking, please let me know.

this

for k in format_data.keys():     if k == 'text':         text = format_data['text'] 

could rewritten as:

if 'text' in format_data:     text = format_data['text'] 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -