Reputation: 505
I need to convert a shell output which is a string to python in order to perform manipulation in that. For reference, my code is shown below.
p = Popen('sudo df -h /var/lib/docker/volumes/myvolume2/_data', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = p.stdout.read()
print (output)
Current Output: Filesystem Size Used Avail Use% Mounted on /dev/sda6 109G 32G 72G 31% /
Expected Output
```{
'Filesystem' :/dev/sda6,
'Size': 109G,
'Used': 32G,
'Use%': 31%
}```
Upvotes: 0
Views: 634
Reputation: 17247
You could build a simple ad-hoc parser just with the basic tools:
p = Popn(...)
output = p.stdout.read().decode() # from bytes to str
header, data, *_ = output.split('\n') # split to lines
header = header.replace('Mounted on', 'Mounted_on') # fix a two word field
df = dict(zip(header.split(), data.split())) # split to fields and combine
print (df)
Upvotes: 1