在Python中加载大型JCON文件-Error=JSONDecodeError:额外数据
我正在尝试从yelp加载python文件business.json 学术挑战可用的学术数据,见下文 ()我的目标是提取所有餐厅及其ID,然后找到我喜欢的一家餐厅 我对……感兴趣。一旦我有了这家餐厅的id,我就要上菜了 json并提取给定餐厅的所有评论。可悲的是我 我被困在登陆.json的初始阶段 business.json就是这样的:在Python中加载大型JCON文件-Error=JSONDecodeError:额外数据,python,json,yelp,Python,Json,Yelp,我正在尝试从yelp加载python文件business.json 学术挑战可用的学术数据,见下文 ()我的目标是提取所有餐厅及其ID,然后找到我喜欢的一家餐厅 我对……感兴趣。一旦我有了这家餐厅的id,我就要上菜了 json并提取给定餐厅的所有评论。可悲的是我 我被困在登陆.json的初始阶段 business.json就是这样的: { // string, 22 character unique string business id "business_id": "tnhfD
{
// string, 22 character unique string business id
"business_id": "tnhfDv5Il8EaGSXZGiuQGg",
// string, the business's name
"name": "Garaje",
// string, the neighborhood's name
"neighborhood": "SoMa",
// string, the full address of the business
"address": "475 3rd St",
// string, the city
"city": "San Francisco",
// string, 2 character state code, if applicable
"state": "CA",
// string, the postal code
"postal code": "94107",
// float, latitude
"latitude": 37.7817529521,
// float, longitude
"longitude": -122.39612197,
// float, star rating, rounded to half-stars
"stars": 4.5,
// interger, number of reviews
"review_count": 1198,
// integer, 0 or 1 for closed or open, respectively
"is_open": 1,
// object, business attributes to values. note: some attribute values might be objects
"attributes": {
"RestaurantsTakeOut": true,
"BusinessParking": {
"garage": false,
"street": true,
"validated": false,
"lot": false,
"valet": false
},
},
// an array of strings of business categories
"categories": [
"Mexican",
"Burgers",
"Gastropubs"
],
// an object of key day to value hours, hours are using a 24hr clock
"hours": {
"Monday": "10:00-21:00",
"Tuesday": "10:00-21:00",
"Friday": "10:00-21:00",
"Wednesday": "10:00-21:00",
"Thursday": "10:00-21:00",
"Sunday": "11:00-18:00",
"Saturday": "10:00-21:00"
}
}
当我尝试使用以下代码导入business.json时:
import json
jsonBus = json.loads(open('business.json').read())
for item in jsonBus:
name = item.get("Name")
businessID = item.get("business_id")
我得到以下错误:
runfile('/Users/Nico/Google Drive/Python/yelp/yelp_academic.py', wdir='/Users/Nico/Google Drive/Python/yelp')
Traceback (most recent call last):
File "<ipython-input-46-68ba9d6458bc>", line 1, in <module>
runfile('/Users/Nico/Google Drive/Python/yelp/yelp_academic.py', wdir='/Users/Nico/Google Drive/Python/yelp')
File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 710, in runfile
execfile(filename, namespace)
File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/Nico/Google Drive/Python/yelp/yelp_academic.py", line 3, in <module>
jsonBus = json.loads(open('business.json').read())
File "/anaconda3/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/anaconda3/lib/python3.6/json/decoder.py", line 342, in decode
raise JSONDecodeError("Extra data", s, end)
JSONDecodeError: Extra data
runfile('/Users/Nico/Google-Drive/Python/yelp/yelp_-academic.py',wdir='/Users/Nico/Google-Drive/Python/yelp')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('/Users/Nico/Google-Drive/Python/yelp/yelp_-academic.py',wdir='/Users/Nico/Google-Drive/Python/yelp')
文件“/anaconda3/lib/python3.6/site packages/spyder/utils/site/sitecustomize.py”,第710行,在runfile中
execfile(文件名、命名空间)
文件“/anaconda3/lib/python3.6/site packages/spyder/utils/site/sitecustomize.py”,第101行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“/Users/Nico/googledrive/Python/yelp/yelp_academical.py”,第3行,在
jsonBus=json.load(open('business.json').read())
文件“/anaconda3/lib/python3.6/json/_init__.py”,第354行,加载
返回\u默认\u解码器。解码
文件“/anaconda3/lib/python3.6/json/decoder.py”,第342行,解码中
raise JSONDecodeError(“额外数据”,s,结束)
JSONDecodeError:额外数据
有人知道为什么会出现这样的错误吗
我也愿意接受任何更聪明的方法
最好的
Nico如果您的json文件与您提到的完全相同,那么它不应该有注释(也称为
//字符串,22个字符的唯一字符串业务id
),因为它们不是标准的一部分
请参阅此处的相关帖子:我认为这是可行的-我使用的是相同的数据集,并且有类似的错误。看到一条似乎有用的评论
import json
js = [json.loads(line) for line in open('business.json')]
for item in js:
name = item.get("name")
businessID = item.get("business_id")
然而,我仍然想知道为什么
json.loads()
不起作用。文件本身看起来很好。这是yelp网站上的复制粘贴,我想它不在jsonI中。我使用相同的数据集,得到的错误与OP相同。我将文件中唯一的/
替换为“aka”()。JSON看起来是合法的,否则没有注释。OP展示它的方式,就是从这个链接。实际上,这并不是数据在文件中的布局方式。通过使用json_data=json.loads('business.json')
我得到了几乎相同的错误,它是从None\n json.decoder.jsondecodecor.jsondecodecor:期望值:第1行第1列(char 0)
json.loads()
加载一个字符串,不是文件,希望该文件是一个完整的JSON对象。相反,该文件在每个节点上都包含一个JSON对象line@cricket_007-ooohhhh好的-我对json是新手(显然是:P),但我没有意识到这一点。谢谢你的留言!!