开发者社区> 问答> 正文

Python在读取JSON文件时抛出错误

我有一些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)

这背后的原因是“纬度”字段缺少值,该字段既不是空字符串,也不是空字符串(因为源API中的数据类型是这个字段的双倍)。有人能建议一种方法来打开这个文件并在json maodule中加载它吗。

PS:我也尝试打开文件,并使用josn.dump(file_obj.read()),但问题是它也接受了dump()中的所有回车。

展开
收起
几许相思几点泪 2019-11-29 18:04:14 2747 0
1 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载