我正在使用Ipython进行一些数据分析,我无法加载JSON文件。请帮我在IPython中加载这个JSON文件。 我还希望在第一行中跳过相同的单词以使其成为一种干净的格式,我希望每条记录都是这样的:
{"station_id":"72","num_bikes_available":18,"num_bikes_disabled":0,"num_docks_available":20,"num_docks_disabled":1,"is_installed":1,"is_renting":1,"is_returning":1,"last_reported":"1467164372","eightd_has_available_keys":false},
这是我的代码:
In [9]: path = 'stationstatus.json'
In [10]: records = [json.loads(line) for line in open(path)]
这是错误:
JSONDecodeError Traceback (most recent call last)
<ipython-input-10-b1e0b494454a> in <module>()
----> 1 records = [json.loads(line) for line in open(path)]
<ipython-input-10-b1e0b494454a> in <listcomp>(.0)
----> 1 records = [json.loads(line) for line in open(path)]
//anaconda/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
317 parse_int is None and parse_float is None and
318 parse_constant is None and object_pairs_hook is None and not kw):
--> 319 return _default_decoder.decode(s)
320 if cls is None:
321 cls = JSONDecoder
//anaconda/lib/python3.5/json/decoder.py in decode(self, s, _w)
340 end = _w(s, end).end()
341 if end != len(s):
--> 342 raise JSONDecodeError("Extra data", s, end)
343 return obj
344
以下是我的JSON文件的一部分**:
{
"last_updated": 1467164806,
"ttl": 10,
"data": {
"stations": [{
"station_id": "72",
"num_bikes_available": 18,
"num_bikes_disabled": 0,
"num_docks_available": 20,
"num_docks_disabled": 1,
"is_installed": 1,
"is_renting": 1,
"is_returning": 1,
"last_reported": "1467164372",
"eightd_has_available_keys": false
}, {
"station_id": "79",
"num_bikes_available": 1,
"num_bikes_disabled": 2,
"num_docks_available": 30,
"num_docks_disabled": 0,
"is_installed": 1,
"is_renting": 1,
"is_returning": 1,
"last_reported": "1467163375",
"eightd_has_available_keys": false
}, {
"station_id": "82",
"num_bikes_available": 3,
"num_bikes_disabled": 3,
"num_docks_available": 21,
"num_docks_disabled": 0,
"is_installed": 1,
"is_renting": 1,
"is_returning": 1,
"last_reported": "1467161631",
"eightd_has_available_keys": false
}, {
"station_id": "83",
"num_bikes_available": 36,
"num_bikes_disabled": 0,
"num_docks_available": 26,
"num_docks_disabled": 0,
"is_installed": 1,
"is_renting": 1,
"is_returning": 1,
"last_reported": "1467163453",
"eightd_has_available_keys": false
}, {
"station_id": "116",
"num_bikes_available": 5,
"num_bikes_disabled": 3,
"num_docks_available": 31,
"num_docks_disabled": 0,
"is_installed": 1,
"is_renting": 1,
"is_returning": 1,
"last_reported": "1467164693",
"eightd_has_available_keys": false
}, {
"station_id": "119",
"num_bikes_available": 15,
"num_bikes_disabled": 0,
"num_docks_available": 4,
"num_docks_disabled": 0,
"is_installed": 1,
"is_renting": 1,
"is_returning": 1,
"last_reported": "1467160413",
"eightd_has_available_keys": false
}]
}
}
答案 0 :(得分:2)
以下是加载文件的建议:
with open('Path/to/file', 'r') as content_file:
content = content_file.read()
records = json.loads(content)
json中的根对象将位于records
变量