标签:test1 name python JSONDecodeError json template test line root
有如下一个文件,内容如下
{ "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
问题解决!!!
标签:test1,name,python,JSONDecodeError,json,template,test,line,root 来源: https://www.cnblogs.com/bainianminguo/p/12028301.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。