我有一个类将包含字符串的列表转储到 .json 文件中,另一个类则加载此文件。但是,我收到了我不明白的奇怪错误 - 这是 .json 文件的内容(示例): ["1", "1", "1", "1", "1", "1", "1", "11", "1", "1", "1.1.1.1", "11.11.11.11"]
这是读取另一个类中的 .json 文件的代码:
with open('test.json','a+') as infile:
if os.stat('test.json').st_size != 0:
jsonimport = json.load(infile)
我在想,如果文件不存在,Python 中的 open() 不会创建文件,但我没有正常读取文件,而是使用 JSON 加载数据...
这是错误:
File "C:\Users\Administrator\Documents\QTTestGIT\IPv4calc.py", line 12, in __init__
self.CallPrompt()
File "C:\Users\Administrator\Documents\QTTestGIT\IPv4calc.py", line 18, in CallPrompt
jsonimport = json.load(infile)
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 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Process finished with exit code 1
相关分类