使用Python Pandas读取JSON文件的调试

时间:2016-03-02 19:41:19

标签: python json pandas

当我试图用Pandas.read_json简单地读取JSON文件时,我陷入了困境。当我尝试使用此示例数据集时,它非常棒。

import pandas as pd
df = pd.read_json('sample.json')

我的示例JSON文件如下所示:

[{"field1": "King's Landing", "field2": 4, "field3": "2014-01-25", "field4": 4.7, "field5": 1.1, "field6": "2014-06-17", "field7": "iPhone", "field8": 15.4, "field9": true, "field10": 46.2, "field11": 3.67, "field12": 5.0}, {"field1": "Astapor", "field2": 0, "field3": "2014-01-29", "field4": 5.0, "field5": 1.0, "field6": "2014-05-05", "field7": "Android", "field8": 0.0, "field9": false, "field10": 50.0, "field11": 8.26, "field12": 5.0}, {"field1": "Astapor", "field2": 3, "field3": "2014-01-06", "field4": 4.3, "field5": 1.0, "field6": "2014-01-07", "field7": "iPhone", "field8": 0.0, "field9": false, "field10": 100.0, "field11": 0.77, "field12": 5.0}]

不幸的是,当我刚尝试用我的完整数据集替换文件名时,它会返回以下错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-63-02c20a7d81eb>", line 1, in <module>
df1 = pd.read_json('train.json')
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/io/json.py", line 210, in read_json
date_unit).parse()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/io/json.py", line 278, in parse
self._parse_no_numpy()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/io/json.py", line 495, in _parse_no_numpy
loads(json, precise_float=self.precise_float), dtype=None)
  ValueError: Expected object or value

任何人都可以帮我调试它为什么这么说?

1 个答案:

答案 0 :(得分:2)

我猜你拼错了你的JSON文件名......

以下脚本给出了完全相同的错误消息:

import pandas as pd

df = pd.read_json('THERE_IS_NO_SUCH_FILE.json')

您可能还想验证您的JSON文件here

如果您的JSON文件太大而无法在线解析,请尝试以下操作:

python -m json.tool your_json_file.json

它应该显示第一次解析/验证错误发生的地方