简体   繁体   English

For循环未遍历字典的所有值

[英]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. 我正在尝试从网页( http://python-data.dr-chuck.net/comments_295023.json )检索数据,使用JSON在Python中解析数据,并对它执行一些操作。 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. 如果我在程序的前面打印url_read似乎工作正常。 So does the json.loads() portion; json.loads()部分也是如此; 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. 我不知道为什么for循环会给我我得到的输出。 Any help would be appreciated! 任何帮助,将不胜感激!

Your first example worked just fine . 您的第一个示例工作得很好 You are iterating over a dictionary , one with two keys, 'note' and 'comments' . 您正在遍历一本字典 ,其中有两个键,分别是'note''comments' Iteration yields those keys, and you printed them. 迭代产生这些键,然后将它们打印出来。

Just access either key: 只需访问任一键:

print info['comments']
print info['note']

or loop over info.items() to get both key and value in each iteration. 或在info.items()上循环以在每次迭代中获取键和值。

The 'comments' value is a list of dictionaries, each with a 'count' and 'name' key: 'comments'值是一个词典列表,每个词典都有一个'count''name'键:

for comment in info['comments']:
    print comment['name'], comment['count']

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']

As I saw in you link is your data struct a dict of lists of dicts. 正如我在您所看到的那样,链接是您的数据结构由字典列表组成的字典。 Use 采用

for key, item in info.iteritems()

This will supply th output you need. 这将提供您需要的输出。 Note that for the inner dicts you need another loop. 请注意,对于内部字典,您需要另一个循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM