Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to fix a KeyError in django?
    primarykey
    data
    text
    <p><code>print user_dic[id]</code> displays the right result <code>PersonA</code>. This is when I input the id manually.</p> <pre><code>user_stream = {u'2331449': u'PersonB', u'17800013': u'PersonA'} user_dic= {} for item in user_stream: user_dic[item['id']] = item['name'] id = '17800013' print user_dic[id] #returns the right value </code></pre> <p>However, when I try to put the <code>user_id</code> through a for loop that iterates through json I get an error: <code>KeyError at 17800013</code> for the line <code>name = user_dic[user_id]</code>. I don't understand why the <code>user_dic[id]</code> works when manually inputting the id, but <code>user_dic[user_id]</code> doesn't work when going through the for loop even though the input is the same. </p> <pre><code>#right fql query fql_query = "SELECT created_time, post_id, actor_id, type, updated_time, attachment FROM stream WHERE post_id in (select post_id from stream where ('video') in attachment AND source_id IN ( SELECT uid2 FROM friend WHERE uid1=me()) limit 100)" fql_var = "https://api.facebook.com/method/fql.query?access_token=" + token['access_token'] + "&amp;query=" + fql_query + "&amp;format=json" data = urllib.urlopen(fql_var) fb_stream = json.loads(data.read()) fb_feed = [] for post in fb_stream: user_id = post["actor_id"] name = user_dic[user_id] #this is the line giving me trouble title = post["attachment"]["name"] link = post["attachment"]["href"] video_id = link[link.find('v=')+2 : link.find('v=')+13] fb_feed.append([user_id, name, title, video_id]) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload