2016-07-11 14 views
0

私はウェブページ(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()部分も同様です。それが私に与える辞書には、ウェブページからのすべての値が含まれています。

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

明らかに必要なすべてのデータが含まれています

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ループが私に出力を与えているのか分かりません。どんな助けもありがとう!

+0

あなたの最初の例**は**働いています。 JSONペイロードから* dictionary *をロードして、反復であなたにキーを与えました。 'info ['comments']'や 'info ['note']'を使って値を読み込みます。 –

+0

それは働いた!ありがとう! – thevioletsaber

答えて

0

ほとんどのデータは「コメント」キーの内側にあるためです。この

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

あなたの最初の例では、うまくを働いてみてください。 辞書(2つのキー、'note'および'comments')を繰り返しています。反復はそれらのキーを生成し、それらを印刷しました。

ただ、キーのいずれかにアクセス:各繰り返しで、キーと値の両方を取得するためにinfo.items()オーバー

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

またはループを。

'comments'値は辞書のリストで、'count''name'キーを持つ各:

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

私はあなたで見たように、リンクは、あなたのデータはdictsのリストの辞書を構造体です。使用

for key, item in info.iteritems() 

これは必要な出力を供給します。内側のディクテーションには別のループが必要であることに注意してください。

関連する問題