python - python中json文件的加载问题

标签 python

json文件内容如下:

{"votes": {"funny": 0, "useful": 5, "cool": 2}, "user_id": "rLtl8ZkDX5vH5nAx9C3q5Q", "review_id": "fWKvX83p0-ka4JS3dc6E5A", "stars": 5, "date": "2011-01-26", "text": "My wife took me here on my birthday for breakfast and it was excellent.  It looked like the place fills up pretty quickly so the earlier you get here the better.\n\nDo yourself a favor and get their Bloody Mary. It came with 2 pieces of their griddled bread with was amazing and it absolutely made the meal complete.  It was the best \"toast\" I've ever had.\n\nAnyway, I can't wait to go back!", "type": "review", "business_id": "9yKzy9PApeiPPOUJEtnvkg"}
{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "0a2KyEL0d3Yb1V6aivbIuQ", "review_id": "IjZ33sJrzXqU-0X6U8NwyA", "stars": 5, "date": "2011-07-27", "text": "I have no idea why some people give bad reviews about this place. It goes to show you, you can please everyone. They are probably griping about something that their own fault... but they said we'll be seated when the girl comes back from seating someone else. So, everything was great and not like these bad reviewers. That goes to show you that  you have to try these things yourself because all these bad reviewers have some serious issues.", "type": "review", "business_id": "ZRJwVLyzEJq1VAihDhYiow"}

我的代码是:

import json
from pprint import pprint
review = open('/User/Desktop/python/test.json')
data = json.load(review)
pprint(data["votes"])

错误是:

Traceback (most recent call last):
  File "/Users/hadoop/Documents/workspace/dataming-course/src/Yelp/main.py", line 8, in <module>
    data = json.load(review)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 278, in load
    **kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 363, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 3 column 1 (char 623 - 1294)

最佳答案

您在一个文件中有两个 JSON 文档。考虑将它们放入数组或其他东西中。文件的顶层应该只包含一个元素。

关于python - python中json文件的加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16259427/

相关文章:

python - 使用 iframe 抓取网站

python多处理队列错误

python - 根据其他列中的值对列中数据框行的子集应用函数

python - 在 ipdb shell 中使用 IPython 魔术函数

python - MATLAB 和 Python 中的逆矩阵结果不同

python - 沿 numpy 数组应用函数

python - OpenCV 3.0 线迭代器

python - Keras回调导致错误: You must feed a value for placeholder tensor 'conv2d_1_input' with dtype float

python - 字典解包过程中内部发生了什么?

python - 对一个函数使用不同的文件,但由于使用 with 而导致 I/O 错误