gpt4 book ai didi

json - 将 JSON 文件读入 Pandas 时出错

转载 作者:行者123 更新时间:2023-12-01 08:03:19 25 4
gpt4 key购买 nike

我正在尝试将 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 添加到参数中,但这并没有解决问题。我欢迎有关如何处理此错误的建议。

最佳答案

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

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

关于json - 将 JSON 文件读入 Pandas 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35329672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com