0

I am trying to get the cpu utilization of my Raspberry Pi through a Python program.

The following bash statement works great:

top -n1 | grep %Cpu    
%Cpu(s): 35.6 us, 15.6 sy,  0.0 ni, 47.3 id,  0.1 wa,  0.0 hi,  1.4 si,  0.0 st

However, when I try to cut the piece of information I need in my Python program, something weird happens. The left delimiter works great, however the right one makes my result disappear (only blanks are returned)

def get_cpu_utilization():
    statement = "top -n1 | grep %Cpu"
    result = check_output(statement, shell=True)
    # result = result[8:]  this works!
    # result = result[:14] doesn't work!
    #The statement below doesn't work either 
    result = result[8:14]
    print(result)

Again all I get are blanks...

What am I doing wrong here?

EDITED 1:

Running the code on my Mac works fine:

Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> result = "%Cpu(s): 39.3 us, 15.8 sy,  0.0 ni, 43.4 id,  0.1 wa,  0.0 hi,  1.3 si,  0.0 st"
>>> print(result[8:14])
 39.3 
>>> 

EDITED 2:

A step by step for you to see what happens:

from subprocess import check_output


def get_cpu_utilization():
    statement = "top -n1 | grep %Cpu"
    result = check_output(statement, shell=True)
    print(result)
    result = result[8:]
    print(result)
    result = result[:6]
    print(result)
    result = result.strip()
    print repr(result)
    return result

This is what I get:

me@rpi $ sudo python cpu.py
%Cpu(s): 30.8 us, 15.2 sy,  0.0 ni, 52.6 id,  0.1 wa,  0.0 hi,  1.3 si,  0.0 st

 30.8 us, 15.2 sy,  0.0 ni, 52.6 id,  0.1 wa,  0.0 hi,  1.3 si,  0.0 st



me@rpi $ 
17
  • What doesn't work - what is your expected output? I get result[8:14] == 35.6. Commented Jan 17, 2016 at 16:59
  • The temperature. Just like you posted. Commented Jan 17, 2016 at 17:01
  • Can you try checking the result object before cutting it? Commented Jan 17, 2016 at 17:03
  • %Cpu(s): 39.3 us, 15.8 sy, 0.0 ni, 43.4 id, 0.1 wa, 0.0 hi, 1.3 si, 0.0 st Commented Jan 17, 2016 at 17:03
  • 2
    top may use ansi escape codes to format it's output unless you use the -b (batch) flat, so you may want to do so. This escape sequences don't show up when printed directly to the terminal, like Martijn said, use repr() to check. Commented Jan 17, 2016 at 17:34

1 Answer 1

1

There seem to be some special characters in between. Generally using fixed indices for this problem does not seem to be very good, because sometimes you might also have lesser digits.

I used the following method which works nicely:

statement = "top -n1 | grep %Cpu"
result = check_output(statement, shell=True).split()
print result[1] // this is the string representing the value you want
print float(result[1]) // conversion to float works, in case you want 

to compute something from it

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.