0

I want to get a specific row-column element from string.

import subprocess
process = subprocess.Popen(['free','-m'],stdout=subprocess.PIPE)
out, err = process.communicate()
out = out.decode("utf-8")
print(out)

Output is :

              total        used        free      shared  buff/cache   available
Mem:           3854        2778         299         351         776         407
Swap:          3909          80        3829

I want to get 3rd row, 3nd column element , that is 80. How can i get that ?

1
  • 1
    split on lines then split lines, pick 3rd element Commented Oct 5, 2017 at 8:07

1 Answer 1

1

once decoded, split according to the lines, then pick the interesting line, split using str.split and pick the relevant field. Convert to integer:

output = """              total        used        free      shared  buff/cache   available
Mem:           3854        2778         299         351         776         407
Swap:          3909          80        3829"""

print(int(output.splitlines()[2].split()[2]))

that gives 80 as expected

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.