-1

私のコードは

import json
import urllib2


# bit.ly data path
bitly_data_path = 'http://bitly.measuredvoice.com/bitly_archive/usagov_bitly_data2012-10-31-1351662469'

file = urllib2.urlopen(bitly_data_path)
records = [json.dumps(json.loads(line)) for line in file]
type(records[0])

これを実行すると、

Out[32]: str

最後の行を

type(dict(records[0]))

私は得る

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/hhimanshu/code/p/python/notebooks/<ipython-input-33-eb9c026d2b46> in <module>()
      8 file = urllib2.urlopen(bitly_data_path)
      9 records = [json.dumps(json.loads(line)) for line in file]
---> 10 type(dict(records[0]))

ValueError: dictionary update sequence element #0 has length 1; 2 is required

urlのデータは次のようになります...

{ "a": "Mozilla\/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident\/4.0)", "c": "US", "nk": 1, "tz": "America\/Chicago", "gr": "MO", "g": "f8zxQx", "h": "OYt09h", "l": "fhah03269", "hh": "bit.ly", "r": "direct", "u": "http:\/\/portal.hud.gov\/hudportal\/HUD?src=\/i_want_to\/talk_to_a_housing_counselor", "t": 1351662469, "hc": 1350056673, "kw": "HUDcounsel", "cy": "Saint Louis", "ll": [ 38.639900, -90.183998 ] }
{ "a": "Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko\/20100101 Firefox\/14.0.1", "c": "US", "nk": 1, "tz": "America\/Los_Angeles", "gr": "WA", "g": "YlMtB7", "h": "YlMtB6", "l": "twitterfeed", "al": "en-us,en;q=0.5", "hh": "1.usa.gov", "r": "http:\/\/www.facebook.com\/l.php?u=http%3A%2F%2F1.usa.gov%2FYlMtB6&h=1AQFYTwLaAQFVlA4rmQgAET0ZHeNpBYEtVPYb18UJmHGjPQ&s=1", "u": "http:\/\/alerts.weather.gov\/cap\/wwacapget.php?x=OR124CCAE88F2C.WindAdvisory.124CCAE9CD24OR.MFRNPWMFR.b3351cd23df7ee2759f052f670a174df&utm_medium=facebook&utm_source=twitterfeed", "t": 1351662473, "hc": 1351661843, "cy": "Poulsbo", "ll": [ 47.753700, -122.612297 ] }
{ "a": "Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1)", "c": "US", "nk": 0, "tz": "America\/Indianapolis", "gr": "IN", "g": "rsE6tG", "h": "oeow2l", "l": "addthis", "hh": "bit.ly", "r": "direct", "u": "http:\/\/www.gsa.gov\/portal\/content\/104109#.TjjjzkOlPCo.twitter", "t": 1351662478, "hc": 1312336782, "cy": "Indianapolis", "ll": [ 39.806198, -86.140701 ] }
{ "a": "Mozilla\/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident\/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)", "c": "RU", "nk": 1, "tz": "Europe\/Moscow", "gr": "48", "g": "Q33nV2", "h": "TQWLo1", "l": "jerrybrown2010", "al": "ru", "hh": "bit.ly", "r": "http:\/\/yandex.ru\/yandsearch?text=bit.ly&lr=213", "u": "http:\/\/gov.ca.gov\/news.php?id=17800", "t": 1351662480, "hc": 1351565210, "cy": "Moscow", "ll": [ 55.752201, 37.615601 ] }
{"_heartbeat_":1351662481}
...

json自分のコンテンツをレコードに変更するにはどうすればよいですか?

4

2 に答える 2

2

文字列から辞書を作成しようとしています。最後に行った json.dumps 呼び出しをそのままにしておきます。dumps辞書を文字列に変換します。loads文字列を辞書に変換します。

これはあなたの行です:

records = [json.loads(line) for line in file]
于 2012-10-31T06:58:32.120 に答える
1
json_data = urllib2.urlopen(bitly_data_path)
data = json.loads(json_data.read())
于 2012-10-31T06:58:16.200 に答える