无法在Python中将数据从json文件加载到dict
我有一部分代码用于将嵌套字典保存到带有缩进6的json文件中,现在在另一个程序中,我必须读取相同的文件并将其保存到字典中。它正在失败 这是用于保存的代码段无法在Python中将数据从json文件加载到dict,python,json,api,Python,Json,Api,我有一部分代码用于将嵌套字典保存到带有缩进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(
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
文件“C:\Users\chaudsup\Desktop\python converter\temp.py”,第13行,在
dict1=json.loads(检查)
文件“C:\Users\chaudsup\Anaconda3\lib\json\\ uuuuu init\uuuuu.py”,第348行,加载
返回\u默认\u解码器。解码
文件“C:\Users\chaudsup\Anaconda3\lib\json\decoder.py”,第337行,在decode中
obj,end=self.raw\u decode(s,idx=\u w(s,0.end())
文件“C:\Users\chaudsup\Anaconda3\lib\json\decoder.py”,第355行,原始解码
从None引发JSONDecodeError(“预期值”,s,err.value)
JSONDecodeError:应为值
加载json时使用此语法
with open('myfile.json') as f:
data = json.load(f)
加载json时使用此语法
with open('myfile.json') as f:
data = json.load(f)
尝试它已经给出了错误:TypeError:JSON对象必须是str、bytes或bytearray,而不是textioWrapper。这起作用了,我后来看到您删除了“r”。非常感谢。它已经给出了错误:TypeError:JSON对象必须是str、bytes或bytearray,而不是textioWrapper。这起作用了,我后来看到您删除了“r”。谢谢。