thevioletsaber thevioletsaber - 4 months ago 8
Python Question

For loop not iterating through all values of a dictionary

I'm trying to retrieve data from a webpage (http://python-data.dr-chuck.net/comments_295023.json), parse it in Python using JSON, and perform some operations on it. However, I've run into a roadblock.

This is my code:

import urllib
import json

address = raw_input('Enter location: ')
if len(address) < 1 :
url = "http://python-data.dr-chuck.net/comments_295023.json"
print 'Retrieving', url
url_open = urllib.urlopen(url)
data_str = url_open.read()
print 'Retrieved', len(data_str), 'characters'

info = json.loads(data_str)
#print info
for item in info:
print item


This is the output I get from the code:

Enter location:
Retrieved 2744 characters
note
comments


The url_read seems to be working fine if I print it earlier in the program. So does the
json.loads()
portion; the dictionary it gives me contains all the values from the webpage.

import urllib
import json

address = raw_input('Enter location: ')
if len(address) < 1 :
url = "http://python-data.dr-chuck.net/comments_295023.json"
print 'Retrieving', url
url_open = urllib.urlopen(url)
data_str = url_open.read()
print 'Retrieved', len(data_str), 'characters'

info = json.loads(data_str)
print info


gives me the output:

Enter location:
Retrieved 2744 characters
{u'note': u'This file contains the actual data for your assignment', u'comments': [{u'count': 98, u'name': u'Maxim'}, {u'count': 98, u'name': u'Amelia'}, {u'count': 90, u'name': u'Sandra'}, {u'count': 89, u'name': u'Betane'}, {u'count': 89, u'name': u'Sanaa'}, {u'count': 88, u'name': u'Nerisse'}, {u'count': 88, u'name': u'Kaisha'}, {u'count': 86, u'name': u'Kelum'}, {u'count': 80, u'name': u'Pardeepraj'}, {u'count': 80, u'name': u'Meri'}, {u'count': 80, u'name': u'Garry'}, {u'count': 78, u'name': u'Beth'}, {u'count': 76, u'name': u'Pamindar'}, {u'count': 74, u'name': u'Jace'}, {u'count': 71, u'name': u'Arman'}, {u'count': 71, u'name': u'Scout'}, {u'count': 65, u'name': u'Atiya'}, {u'count': 65, u'name': u'Alani'}, {u'count': 65, u'name': u'Sajjad'}, {u'count': 64, u'name': u'Jedidiah'}, {u'count': 63, u'name': u'Patryk'}, {u'count': 61, u'name': u'Alyshia'}, {u'count': 60, u'name': u'Michaela'}, {u'count': 58, u'name': u'Rowanna'}, {u'count': 54, u'name': u'Anabelle'}, {u'count': 52, u'name': u'Corah'}, {u'count': 49, u'name': u'Ninon'}, {u'count': 45, u'name': u'Kristal'}, {u'count': 37, u'name': u'Kerryanne'}, {u'count': 35, u'name': u'Saarah'}, {u'count': 35, u'name': u'Diego'}, {u'count': 31, u'name': u'Damaris'}, {u'count': 30, u'name': u'Ryleigh'}, {u'count': 26, u'name': u'Kaley'}, {u'count': 22, u'name': u'Maariyah'}, {u'count': 22, u'name': u'Cheyenne'}, {u'count': 21, u'name': u'Jazmine'}, {u'count': 19, u'name': u'Shaarvin'}, {u'count': 19, u'name': u'Loulou'}, {u'count': 19, u'name': u'Oluwafemi'}, {u'count': 19, u'name': u'Samanthalee'}, {u'count': 17, u'name': u'Ege'}, {u'count': 13, u'name': u'Clarke'}, {u'count': 8, u'name': u'Hubert'}, {u'count': 7, u'name': u'Scarlet'}, {u'count': 7, u'name': u'Kellen'}, {u'count': 4, u'name': u'Roark'}, {u'count': 3, u'name': u'Kinsey'}, {u'count': 3, u'name': u'Tansy'}, {u'count': 1, u'name': u'Aymal'}]}


which obviously contains all the necessary data.

I have no idea why the for loop is giving me the output I got. Any help would be appreciated!

Answer

It is because most of your data is inside 'comments' key. Try this

for item in info:
    print item
    if item == 'comments':
        for x in info['comments']:
            print x['count']
            print x['name']
Comments