我试图将存储在工作目录中的.json文件读入jupyter笔记本中,以最终将其放入熊猫数据帧中进行清理,但使用json.load函数遇到困难。
数据结构:
{
"URL":"http://www.just-eat.co.uk/restaurants-cn-chinese-cardiff/menu",
"_id":{"$oid":"55f14312c7447c3da7051b26"},
"address":"228 City Road",
"address line 2":"Cardiff",
"name":".CN Chinese",
"outcode":"CF24",
"postcode":"3JH",
"rating":5,
"type_of_food":"Chinese"
}
我已经手动输入了两行数据以对其进行测试,
data = [{"URL":"http://www.just-eat.co.uk/restaurants-cn-chinese-cardiff/menu","_id":{"$oid":"55f14312c7447c3da7051b26"},"address":"228 City Road","address line 2":"Cardiff","name":".CN Chinese","outcode":"CF24","postcode":"3JH","rating":5,"type_of_food":"Chinese"}, {"URL":"http://www.just-eat.co.uk/restaurants-atthai-ss9/menu","_id":{"$oid":"55f14312c7447c3da7051b27"},"address":"376 Rayleigh Road","address line 2":"Essex","name":"@ Thai","outcode":"SS9","postcode":"5PT","rating":5.5,"type_of_food":"Thai"}]
df = pd.DataFrame.from_dict(data, orient='columns')
df
URL _id address address line 2 name outcode postcode rating type_of_food
0 http://www.just-eat.co.uk/restaurants-cn-chine... {u'$oid': u'55f14312c7447c3da7051b26'} 228 City Road Cardiff .CN Chinese CF24 3JH 5.0 Chinese
1 http://www.just-eat.co.uk/restaurants-atthai-s... {u'$oid': u'55f14312c7447c3da7051b27'} 376 Rayleigh Road Essex @ Thai SS9 5PT 5.5 Thai
import json
但是当我尝试使用2种不同的json.load方法读取整个文件时,我得到了相同的错误消息:
import json
with open('restaurant.json') as file:
data = json.load(file)
或
import json
filename= 'restaurant.json'
Jdata= open(filename, 'r+')
print(json.load(Jdata))
我收到以下错误
ValueError Traceback (most recent call last)
<ipython-input-47-128297096a2b> in <module>()
3 filename= 'restaurant.json'
4 Jdata= open(filename, 'r+')
----> 5 print(json.load(Jdata))
/home/bdtech/anaconda2/lib/python2.7/json/__init__.pyc in load(fp, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
289 parse_float=parse_float, parse_int=parse_int,
290 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook,
--> 291 **kw)
292
293
/home/bdtech/anaconda2/lib/python2.7/json/__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
337 parse_int is None and parse_float is None and
338 parse_constant is None and object_pairs_hook is None and not kw):
--> 339 return _default_decoder.decode(s)
340 if cls is None:
341 cls = JSONDecoder
/home/bdtech/anaconda2/lib/python2.7/json/decoder.pyc in decode(self, s, _w)
365 end = _w(s, end).end()
366 if end != len(s):
--> 367 raise ValueError(errmsg("Extra data", s, end, len(s)))
368 return obj
369
ValueError: Extra data: line 2 column 1 - line 2549 column 1 (char 258 - 681632)
谢谢!
答案 0 :(得分:0)
已修复-额外的数据错误是因为它仅读取一行
data = []
with open('restaurant.json') as f:
for line in f:
data.append(json.loads(line))
df = pd.DataFrame.from_dict(data, orient='columns')
按要求将文件中的所有数据读取到数据框中