将JSON文件读入Pandas时出错

时间:2016-02-11 02:17:12

标签: json pandas import

我正在尝试将JSON文件读入Pandas。这是一个相对较大的文件(41k记录),主要是文本。

{"sanders": [{"date": "February 8, 2016 Monday", "source": "Federal News           
Service", "subsource": "MSNBC \"MSNBC Live\" Interview with Sen. Bernie 
Sanders (I-VT), Democratic", "quotes": ["Well, it's not very progressive to     
take millions of dollars from Wall Street as well.", "That's a very good 
question, and I wish I could give her a definitive answer. QUOTE SHORTENED FOR 
SPACE"]}, {"date": "February 7, 2016 Sunday", "source": "CBS News Transcripts", "subsource": "SHOW: CBS FACE THE NATION 10:30 AM EST", "quotes": 
["Well, John -- John, I think that`s a media narrative that goes around and 
around and around. I don`t accept that media narrative.", "Well, that`s what  
she said about Barack Obama in 2008. "]}, 

我试过了:

quotes = pd.read_json("/quotes.json")

我希望它干净利落地阅读,因为它是在python中创建的文件。但是,我收到了这个错误:

ValueError                                Traceback (most recent call last)
<ipython-input-19-c1acfdf0dbc6> in <module>()
----> 1 quotes = pd.read_json("/Users/kate/Documents/99Antennas/Client\    
Files/Fusion/data/quotes.json")

/Users/kate/venv/lib/python2.7/site-packages/pandas/io/json.pyc in   
read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, 
keep_default_dates, numpy, precise_float, date_unit)
    208         obj = FrameParser(json, orient, dtype, convert_axes, 
convert_dates,
    209                           keep_default_dates, numpy, precise_float,
--> 210                           date_unit).parse()
    211 
    212     if typ == 'series' or obj is None:

/Users/kate/venv/lib/python2.7/site-packages/pandas/io/json.pyc in parse(self)
    276 
    277         else:
--> 278             self._parse_no_numpy()
    279 
    280         if self.obj is None:

/Users/kate/venv/lib/python2.7/site-packages/pandas/io/json.pyc in _   
parse_no_numpy(self)
    493         if orient == "columns":
    494             self.obj = DataFrame(
--> 495                 loads(json, precise_float=self.precise_float),   
dtype=None)
    496         elif orient == "split":
    497             decoded = dict((str(k), v)

ValueError: Expected object or value

在阅读文档和stackoverflow之后,我也尝试将convert_dates=False添加到参数中,但这并没有解决问题。我欢迎有关如何处理此错误的建议。

2 个答案:

答案 0 :(得分:4)

尝试删除文件名中的正斜杠。如果你从文件所在的同一目录运行这个python代码,它应该可以工作。

quotes = pd.read_json("quotes.json")

答案 1 :(得分:1)

SPKoder提到了正斜杠。当我意识到我没有添加/当梳理文件名和路径时(即c:/path/herefile.json,而不是c:/path/here/file.json),我正在寻找答案。无论如何,我收到的错误是......

ValueError: Expected object or value

不是非常直观的错误消息,但这就是导致它的原因。