python:json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes問題解決


有如下一個文件,內容如下

{
    "test1": "/root/test/test1.template",
    "test2": "/root/test/test2.template",
    "test3": "/root/test/test3.template",
    "test4": "/root/test/test4.template",
    "test5": "/root/test/test5.template",
    "test6": "/root/test/test6.template",
}

 

 

通過json模塊去實例化上述的文件

import json
import os
import shelve

p = os.path.join(os.path.dirname(os.path.abspath(__file__)),"templatepath")



file = json.load(open(p,"r"))
for k,v in file.items():
    print(v)

 

但是報錯,內容如下

Traceback (most recent call last):
  File "D:/python/test_sip/test_check_es.py", line 323, in <module>
    file = json.load(open(p,"r"))
  File "C:\Program Files\Python36\lib\json\__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Program Files\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\Python36\lib\json\decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 8 column 1 (char 254)

 

百思不得其解,我的文件中都是用的雙引號號啊

其實問題的根源是在文件的最后一行的行尾,多了一個逗號,去掉這個逗號就可以了。

 

修改后的文件內容

{
    "test1": "/root/test/test1.template",
    "test2": "/root/test/test2.template",
    "test3": "/root/test/test3.template",
    "test4": "/root/test/test4.template",
    "test5": "/root/test/test5.template",
    "test6": "/root/test/test6.template"
}

 

再次讀取,內容就可以被正常讀取出來

/root/test/test1.template
/root/test/test2.template
/root/test/test3.template
/root/test/test4.template
/root/test/test5.template
/root/test/test6.template

 

 

問題解決!!!

 

 


免責聲明!

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



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