简体   繁体   中英

How to display key value pair output of a bash command using python

Below is the output neutron-net show command, this is an openstack command output. I want to display value of name,provider:network_type and provider:segmentation value in a single line . I have tried python and mix of bash scripts, but it doesn't work. Any idea, this can be done using 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"
}^

at first, you should make it json type. you can do it with this code:

result = result.replace("}{", "},{")
result = "[" + result + "]"
#result is the output of the command

then convert it to dict:

import json
result_json = json.loads(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM