【发布时间】:2019-11-29 09:52:55
【问题描述】:
我有一些 JSON 文件,内容如下:
{
"name": "",
"street": "",
"street_number": 26,
"district": "VILA MARESIA",
"city": "Raposa",
"state": "",
"country": "BR",
"latitude": ,
"longitude":
}
现在加载这个文件会抛出如下错误:
with open(r"C:\Users\dverma25\menus-folder\menus-folder-MA\rt.json",'r', encoding="utf8") as json_file:
json_data = json.load(json_file)
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-93-8150dd6a3e1d> in <module>
2 logger.info("Start processing the file {}".format(file))
3 # t = json_file.read()
----> 4 json_data = json.load(json_file)
C:\ProgramData\Anaconda3\lib\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
C:\ProgramData\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
C:\ProgramData\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):
C:\ProgramData\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 9 column 15 (char 153)
我知道这背后的原因是“纬度”字段的值丢失,既不是空字符串也不是 None(因为源 API 中的数据类型对于该字段是双精度的)。谁能建议一种打开文件并将其加载到 json maodule 中的方法。
PS:我也尝试打开文件并使用了 josn.dump(file_obj.read()) 但这有一个问题,即它也会在 dump() 中使用所有回车符。
【问题讨论】:
-
您没有 json 文件,因为您发布的不是有效的 json。
-
我知道这不是一个有效的 json,但我从中获取数据的应用程序就是以这种方式设计的。所以真的要找到解决它的方法。
标签: json python-3.x rest dictionary