from a specific subprocess command, i got this output (stdout) bellow:
cmd = 'xx_cmd'
print(subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()[0].decode('utf-8').strip())
i search a way to set this output under a dictionnay:
stdout:
Series# : 123456
diskid : 7788
type_disk : nvme
cap(mo) : 5685
disk_attr : ssd : link : mapped
I search to set the dictionnary as follow:
['Series#':'123456','diskid':'7788','type_disk':'nvme','cap(mo)':'5685','disk_attr': ['ssd','link','mapped']]
Many thanks for any help.
You can use a dictionary comprehension to get the data you want
inp = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()[0].decode('utf-8').strip()
data = {
key: (values if len(values) > 1 else values[0])
for key, *values in [line.split(" : ") for line in inp.splitlines()]
}
{'Series#': '123456', 'diskid': '7788', 'type_disk': 'nvme', 'cap(mo)': '5685', 'disk_attr': ['ssd', 'link', 'mapped']}
for simple testing, you can set the inp
(short for "input", but input
is already a builtin function. you can rename it to whatever you want, though)
inp = """\
Series# : 123456
diskid : 7788
type_disk : nvme
cap(mo) : 5685
disk_attr : ssd : link : mapped"""