我有一部分代码用于将嵌套字典保存到带有缩进6的json文件中,现在在另一个程序中,我必须读取相同的文件并将其保存到字典中。失败了。
这是要保存的代码段
out_file = open("myfile.json", "w")
json.dump(master, out_file, indent = 6)
out_file.close()
我用来阅读的代码段
with open('myfile.json', 'r') as f:
check = f.read()
print(check)
dict1 = json.loads(check)
出于机密性原因,我无法共享正在创建的JSON文件,但是我创建了一个虚拟的json文件进行共享。
{
"ethernet_network": {
"first": {
"name": "q",
"vlanId": "q",
"purpose": "q",
"smartLink": "q",
"privateNetwork": "q",
"subnetUri": "q",
"maximumBandwidth": "q",
"typicalBandwidth": "q"
}
}
}
这是我得到的错误:
File "C:\Users\chaudsup\Desktop\python converter\temp.py", line 13, in <module>
dict1 = json.loads(check)
File "C:\Users\chaudsup\Anaconda3\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\chaudsup\Anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\chaudsup\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
答案 0 :(得分:2)
在加载json时使用此语法
with open('myfile.json') as f:
data = json.load(f)