读取JSON文件时获取解码错误
我试图读取JSON文件,但它给出如下错误 数据参考: 代码 错误:读取JSON文件时获取解码错误,json,python-3.x,Json,Python 3.x,我试图读取JSON文件,但它给出如下错误 数据参考: 代码 错误: --------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) <ipython-input-14-0fce326c8851> in <module> 1
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-14-0fce326c8851> in <module>
1 with open("multiple_levels.json", 'r') as j:
----> 2 contents = json.loads(j.read())
~\AppData\Local\Continuum\anaconda3\lib\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
~\AppData\Local\Continuum\anaconda3\lib\json\decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
~\AppData\Local\Continuum\anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 7 column 1 (char 6)
---------------------------------------------------------------------------
JSONDecodeError回溯(最近一次调用)
在里面
1将open(“multiple_levels.json,'r')作为j:
---->2 contents=json.load(j.read())
加载中的~\AppData\Local\Continuum\anaconda3\lib\json\\uuuuu init\uuuuuuuuuu.py(s、编码、cls、对象钩子、解析浮点数、解析整型、解析常数、对象对钩子、**kw)
346 parse_int为无,parse_float为无且
347 parse_常量为None且对象_pairs_hook为None且非kw):
-->348返回默认解码器。解码
349如果cls为无:
350 cls=JSONDecoder
解码中的~\AppData\Local\Continuum\anaconda3\lib\json\decoder.py(self、s、w)
335
336 """
-->337 obj,end=self.raw\u decode(s,idx=\u w(s,0.end())
338 end=_w(s,end).end()
339如果结束!=长度:
原始解码中的~\AppData\Local\Continuum\anaconda3\lib\json\decoder.py(self、s、idx)
353 obj,end=自扫描一次(s,idx)
354除了停止迭代作为错误:
-->355将JSONDecodeError(“预期值”,s,err.value)从None提升
356返回obj,结束
JSONDecodeError:期望值:第7行第1列(字符6)
问题在于从github下载文件时,没有使用代码。
github中的文件内容与您简单地执行“将链接另存为”时看到的不同
作为解决方案,您可以转到github repo并将其作为zip下载(见附件)
或者,由于文件json data/level-1.json很小,只需复制/粘贴并保存到文本编辑器中即可
这是从github正确下载文件时的结果
下面是文件内容做的一个“保存链接为”。
看看第7行,您的代码在抱怨。问题在于您从github下载文件时,而不是使用代码。 github中的文件内容与您简单地执行“将链接另存为”时看到的不同 作为解决方案,您可以转到github repo并将其作为zip下载(见附件) 或者,由于文件json data/level-1.json很小,只需复制/粘贴并保存到文本编辑器中即可 这是从github正确下载文件时的结果 下面是文件内容做的一个“保存链接为”。
请看第7行,您的代码在那里抱怨。如果使用“原始内容”URL,您可以直接从web加载json:
如果使用“原始内容”URL,您可以直接从web加载json:
我刚刚检查了Python 3.7,您的代码运行良好。我刚刚检查了Python 3.7,您的代码运行良好。
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-14-0fce326c8851> in <module>
1 with open("multiple_levels.json", 'r') as j:
----> 2 contents = json.loads(j.read())
~\AppData\Local\Continuum\anaconda3\lib\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
~\AppData\Local\Continuum\anaconda3\lib\json\decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
~\AppData\Local\Continuum\anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 7 column 1 (char 6)
import json
import urllib.request
url = "https://raw.githubusercontent.com/ankitgoel1602/data-science/master/json-data/multiple_levels.json"
with urllib.request.urlopen(url) as r:
data = json.loads(r.read().decode())
print(data)
[{'Scaler': 'Standard', 'family_min_samples_percentage': 5, 'original_number_of_clusters': 4,
'Results': [{'eps_value': 0.1, 'min_samples': 5, 'number_of_clusters': 9, 'number_of_noise_samples': 72,
'scores': {'adjusted_rand_index': 0.0012946494377947854,
'adjusted_mutual_info_score': 0.008962599716725805, 'homogeneity_score': ...