下面是输出neutron-net show命令,这是openstack命令的输出。我想在一行中显示name,provider:network_type和provider:segmentation值的值。我尝试了python和bash脚本的混合,但是它不起作用。任何想法,都可以使用python完成吗?
# for i in $(neutron net-list -f value -c name); do neutron net-show -c name -c segments -f json $i ; done
{
"segments": "{\"provider:network_type\": \"vxlan\", \"provider:physical_network\": null, \"provider:segmentation_id\": 333}\n{\"provider:network_type\": \"vlan\", \"provider:physical_network\": \"DCGW-NET1\", \"provider:segmentation_id\": 259}",
"name": "_MGMT"
}{
"segments": "{\"provider:network_type\": \"vlan\", \"provider:physical_network\": \"NET1\", \"provider:segmentation_id\": 388}\n{\"provider:network_type\": \"vxlan\", \"provider:physical_network\": null, \"provider:segmentation_id\": 5344}",
"name": "sp1"
}{
"name": "test_emma"
}{
"segments": "{\"provider:network_type\": \"vxlan\", \"provider:physical_network\": null, \"provider:segmentation_id\": 5214}\n{\"provider:network_type\": \"vlan\", \"provider:physical_network\": \"NET1\", \"provider:segmentation_id\": 214}",
"name": "test_MGMT"
}^
答案 0 :(得分:0)
首先,应将其设置为json
类型。您可以使用以下代码进行操作:
result = result.replace("}{", "},{")
result = "[" + result + "]"
#result is the output of the command
然后将其转换为字典:
import json
result_json = json.loads(result)