擅长:python、mysql、java
<p>您可以使用生成器,但我猜您是python新手,这将更简单:</p>
<pre><code>def extract_nodes():
return_data = dict()
for node_datum in json.load(open('/home/ubuntu/slcakbot_openNMS/CLEAR/out.txt'))["node"]:
try:
socket.inet_aton(node_datum["label"])
return_data[node_datum["id"]] = { 'ip': node_datum["label"], 'id': node_datum["id"]}
print(node_datum["label"])
print(node_datum["id"])
#return { 'ip': node_datum["label"], 'id': node_datum["id"]} # i need to return these values
except Exception as err:
print err
pass
return return_data
</code></pre>
<p>至于使用它</p>
<pre><code>node_data = extract_nodes()
for key, node_details in node_data.items():
print node_details['ip'], node_details['id']
</code></pre>