The code snippet prints "cpu usage: us,, <type 'str'>" on my workstation, bumping the splt index to 3 prints the correct sys. usage. What does the header of your 'top' command look like? There's a very good chance it's different than what I'm testing with.
As for the slice, assuming 'splt[2]' is '0.5%sy', then you should be able to remove '%sy' using an indexof -3: Python 2.4.3 (#1, Oct 23 2006, 14:19:47) [GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = "0.5%sy" >>> a[:-3] '0.5' >>> Also note that you may very well have some curses/terminal chars tagged on to that '0.5%sy' string, which causes the slice to return something that you're not expecting. For example: Python 2.4.3 (#1, Oct 23 2006, 14:19:47) [GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> top = os.popen("top -n 1") >>> rd = top.read().split("\n") >>> splt = rd[2].split() >>> print splt ['Cpu(s):\x1b(B\x1b[m\x1b[39;49m\x1b(B\x1b[m', ' 1.2%', '\x1b(B\x1b[m\x1b[39;49mus,\x1b(B\x1b[m\x1b[39;49m\x1b(B\x1b[m', '0.2%', '\x1b(B\x1b[m\x1b[39;49msy,\x1b(B\x1b[m\x1b[39;49m\x1b(B\x1b[m', '0.1%', '\x1b(B\x1b[m\x1b[39;49mni,\x1b(B\x1b[m\x1b[39;49m\x1b(B\x1b[m', ' 98.4%', '\x1b(B\x1b[m\x1b[39;49mid,\x1b(B\x1b[m\x1b[39;49m\x1b(B\x1b[m', '0.1%', '\x1b(B\x1b[m\x1b[39;49mwa,\x1b(B\x1b[m\x1b[39;49m\x1b(B\x1b[m', '0.0%', '\x1b(B\x1b[m\x1b[39;49mhi,\x1b(B\x1b[m\x1b[39;49m\x1b(B\x1b[m', ' 0.0%', '\x1b(B\x1b[m\x1b[39;49msi,\x1b(B\x1b[m\x1b[39;49m\x1b(B\x1b[m', '0\x1b(B\x1b[m\x1b[39;49m\x1b[K'] >>> Jeff Are you sure that splt only contains text? Are there any terminal control characters? On 4/9/07, Pradnyesh Sawant <[EMAIL PROTECTED]> wrote: > Thanks for the pointer to use /proc/stat > However, that does not answer my other question about string slicing > Any pointers in that direction would be helpful... > > Thanks a lot! > > On 4/9/07, Jeff McNeil <[EMAIL PROTECTED]> wrote: > > Can you pull the same information from /proc/stat as opposed to using > > a pipe to top? The first line(s) should contain (at least): > > > > cpu usermode, lowprio, system, idle, hz. > > > > The 2.6 kernel adds iowait, irq, and soft irq. It seems that this > > might be a better solution than executing that additional command. > > > > Take a look at proc(5). > > > > Thanks, > > > > Jeff > > > > > > > > > > > > On 4/9/07, Pradnyesh Sawant <[EMAIL PROTECTED]> wrote: > > > Hello, > > > I need the cpu usage of a linux box, for which i capture the output of > > > "top" using "popen". However, i am facing problems during string > > > handling. The code snippet is:- > > > > > > top = os.popen("top -n 1") > > > rd = top.read().split("\n") > > > splt = rd[2].split() > > > # cpu = splt[2][:-4] # why ain't this working ??? > > > cpu = splt[2] > > > print "cpu usage: %s, %s" % (cpu, type(cpu)) > > > > > > the "print" statement prints, "cpu usage: 0.5%sy, <type 'str'>". i > > > tried my best to get rid of the "%sy" after the "0.5", but couldn't > > > succeed (even though the type of "cpu" is "string"). > > > > > > Also, one weird thing that was happening was that if i still "split" > > > the cpu string (removed the last 4 characters), the output of the next > > > print statement was wrong (specifically, the first 2 characters got > > > chipped off). This output is totally unexpected, and wrong... > > > > > > Can anyone kindly help me in understanding what exactly is going on here. > > > > > > Thanks a lot! > > > -- > > > http://mail.python.org/mailman/listinfo/python-list > > > > > > -- http://mail.python.org/mailman/listinfo/python-list