-1

I get an output of a command like below in str:

Group Name: STI              IP: v4          Comment:
Group Items:  2                Type: User-defined
Members: "vm-1" "vm-2"

I want to be able to convert it into a dictionary so that I get a key-value pair like below:

{"Group Name": "STI", "IP": "v4", "Comment": "", "Group Items": "2", "Type": "User-defined", "Members": '"vm-1" "vm-2"'}

How to achieve the same?

The output got squeezed when I pasted it here, so attaching snippet of the same: enter image description here

jferard
  • 7,835
  • 2
  • 22
  • 35
Satnau
  • 69
  • 1
  • 9
  • 1
    What does the actual string look like? `Group Name: STI IP: v4 Comment: Group Items: 2 `? So space separated? – PyPingu Dec 30 '19 at 15:15
  • @PyPingu- it looks exactly like the screenshot with same amount of spaces as shown – Satnau Dec 30 '19 at 17:40

1 Answers1

0

If the output has always the same format, I guess you best bet is to parse the text like this:

>>> text = '''Group Name: STI              IP: v4          Comment:
... Group Items:  2                Type: User-defined
... Members: "vm-1" "vm-2"'''
>>> text = text.split("\n")
>>> {"Group Name": text[0][12:29], "IP": text[0][33:45], "Comment": text[0][54:], "Group Items": text[1][13:30], "Type": text[1][37:], "Members": text[2][9:]}
{'Group Name': 'STI              ', 'IP': 'v4          ', 'Comment': '', 'Group Items': ' 2               ', 'Type': 'User-defined', 'Members': '"vm-1" "vm-2"'}

You split the text into lines, and then take the slices you need.

If the output may have a different format, that would be a bit more complicated.

jferard
  • 7,835
  • 2
  • 22
  • 35