gpt4 book ai didi

python - 无法在 python 中加载 json 文件

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

我正在尝试从此 link 加载数据集在 python 中,使用 pandas 库的 read_json() 和 python 的内置 json 库来存储在我的计算机中的相同数据集(与我的笔记本相同的文件夹)。以下是我的代码:

import json
import pandas as pd
path='https://s3.amazonaws.com/istarsshare/normround1.json'
data=pd.read_json(path,orient='columns')
data.head(10)

另一个是:

import json
with open("normround1.json", "r") as read_it:
data = json.load(read_it)

在第一种情况下,我收到以下错误:

ValueError                                Traceback (most recent call last) <ipython-input-12-a81b255c7afd> in <module>
1 path='https://s3.amazonaws.com/istarsshare/normround1.json'
----> 2 data=pd.read_json(path,orient='columns')
3 data.head(10)
4 #with open("normround1.json", "r") as read_it:
5 #data = json.load(read_it)

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/_json.py in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression)
590 return json_reader
591
--> 592 result = json_reader.read()
593 if should_close:
594 try:

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/_json.py in read(self)
715 obj = self._get_object_parser(self._combine_lines(data.split("\n")))
716 else:
--> 717 obj = self._get_object_parser(self.data)
718 self.close()
719 return obj

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/_json.py in _get_object_parser(self, json)
737 obj = None
738 if typ == "frame":
--> 739 obj = FrameParser(json, **kwargs).parse()
740
741 if typ == "series" or obj is None:

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/_json.py in parse(self)
847
848 else:
--> 849 self._parse_no_numpy()
850
851 if self.obj is None:

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/_json.py in _parse_no_numpy(self)
1091 if orient == "columns":
1092 self.obj = DataFrame(
-> 1093 loads(json, precise_float=self.precise_float), dtype=None
1094 )
1095 elif orient == "split":

ValueError: Trailing data

在第二种情况下我得到:

---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last) <ipython-input-13-b4c6a39520ca> in <module>
3 #data.head(10)
4 with open("normround1.json", "r") as read_it:
----> 5 data = json.load(read_it)

~/anaconda3/lib/python3.7/json/__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
294 cls=cls, object_hook=object_hook,
295 parse_float=parse_float, parse_int=parse_int,
--> 296 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
297
298

~/anaconda3/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
346 parse_int is None and parse_float is None and
347 parse_constant is None and object_pairs_hook is None and not kw):
--> 348 return _default_decoder.decode(s)
349 if cls is None:
350 cls = JSONDecoder

~/anaconda3/lib/python3.7/json/decoder.py in decode(self, s, _w)
338 end = _w(s, end).end()
339 if end != len(s):
--> 340 raise JSONDecodeError("Extra data", s, end)
341 return obj
342

JSONDecodeError: Extra data: line 2 column 1 (char 161)

我在 Ubuntu 18.04 中使用 jupyter 笔记本

最佳答案

它不是 JSON 文件,而是 multi-JSON 文件 - 每一行都是 JSON 数据。

标准工具无法读取它,您必须单独获取每一行并用作 JSON

import json

data = []

with open('normround1.json') as fh:
for line in fh:
data.append(json.loads(line))

#print(data)
print(data[0]['_id'])
print(data[0]['message'])

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

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