有如下一个文件,内容如下
1 2 3 4 5 6 7 8 | { "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模块去实例化上述的文件
1 2 3 4 5 6 7 8 9 10 11 | 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) |
但是报错,内容如下
1 2 3 4 5 6 7 8 9 10 11 12 | 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 ) |
百思不得其解,我的文件中都是用的双引号号啊
其实问题的根源是在文件的最后一行的行尾,多了一个逗号,去掉这个逗号就可以了。
修改后的文件内容
1 2 3 4 5 6 7 8 | { "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" } |
再次读取,内容就可以被正常读取出来
1 2 3 4 5 6 | / root / test / test1.template / root / test / test2.template / root / test / test3.template / root / test / test4.template / root / test / test5.template / root / test / test6.template |
问题解决!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本