在處理本地json文件的時候,由於json格式的改變,致使代碼不能運行出錯,下面有解決辦法:json
外部連接:http://www.javashuo.com/article/p-xximzffd-hg.htmlspa
【初始代碼】.net
with open(path,'r') as f: a = json.loads(f.read()) print(a) print(type(a))
【提示出錯】code
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)
【解決辦法】!!!blog
with open(path,encoding='utf-8-sig', errors='ignore') as f: data = json.load(f, strict=False) print(data)