我有双dict,我想用json阅读,但我真的不知道如何。我正在运行Python 3
{ "www.svetaine.lt":{ "true": "111.111.222.11" }}
{ "www.svetaine2.lt":{ "true": "111.111.222.11" }}
我只知道如何制作简单的dict
def openfile():
data = []
d={}
with open('test.json', 'r') as f:
for line in f:
data.append(json.loads(line))
for item in data:
d.update(item)
f.close()
return d
所以我想通过使用双dict来改进我的代码。 P.S你能否提一下我在读完之后如何提及它?
for ele in d:
getname="www.svetaine.lt"
getstatus="true"
getip= "111.111.222.11"
答案 0 :(得分:0)
import json
def openfile():
d={}
with open('s.json', 'r') as f:
for line in f:
line_ = json.loads(line)
name = line_.keys()[0]
status = line_[name].keys()[0]
ip = line_[name][status]
d[name] = {'name':name, 'status':status, 'ip':ip}
f.close()
return d
data = openfile()
# print whole dict
for ele in data:
print ele, data[ele]
# retrieving info w.r.t name
print data['www.svetaine.lt']