【python】解决raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)错误

在处理本地json文件的时候,因为json格式的改变,导致代码不能运行出错,下面有解决办法:

外部链接:https://blog.csdn.net/qq_41185868/article/details/82079079

【初始代码】

with open(path,'r') as f:
    a = json.loads(f.read())
    print(a)
    print(type(a))

【提示出错】

Traceback (most recent call last):
  File "C:\Users\14062\Desktop\json02.py", line 25, in <module>
    c = json.loads(b)
  File "C:\Users\14062\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\14062\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\14062\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

【解决办法】!!!

with open(path,encoding='utf-8-sig', errors='ignore') as f:
     data = json.load(f, strict=False)
     print(data)

 

posted @ 2019-08-13 15:25  SYC_792  阅读(89840)  评论(0编辑  收藏  举报