有如下一个文件,内容如下
{
"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
问题解决!!!
浙公网安备 33010602011771号