【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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM