私はウェブページ(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ループが私に出力を与えているのか分かりません。どんな助けもありがとう!
あなたの最初の例**は**働いています。 JSONペイロードから* dictionary *をロードして、反復であなたにキーを与えました。 'info ['comments']'や 'info ['note']'を使って値を読み込みます。 –
それは働いた!ありがとう! – thevioletsaber