繁体   English   中英

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

[英]For loop not iterating through all values of a dictionary

我正在尝试从网页( http://python-data.dr-chuck.net/comments_295023.json )检索数据,使用JSON在Python中解析数据,并对它执行一些操作。 但是,我遇到了一个障碍。

这是我的代码:

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

这是我从代码中得到的输出:

Enter location:
Retrieved 2744 characters
note
comments

如果我在程序的前面打印url_read似乎工作正常。 json.loads()部分也是如此; 它给我的字典包含了网页中的所有值。

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

给我输出:

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

其中显然包含所有必要的数据。

我不知道为什么for循环会给我我得到的输出。 任何帮助,将不胜感激!

您的第一个示例工作得很好 您正在遍历一本字典 ,其中有两个键,分别是'note''comments' 迭代产生这些键,然后将它们打印出来。

只需访问任一键:

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

或在info.items()上循环以在每次迭代中获取键和值。

'comments'值是一个词典列表,每个词典都有一个'count''name'键:

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

这是因为您的大多数数据都在“注释”键内。 尝试这个

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

正如我在您所看到的那样,链接是您的数据结构由字典列表组成的字典。 采用

for key, item in info.iteritems()

这将提供您需要的输出。 请注意,对于内部字典,您需要另一个循环。

暂无
暂无

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

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