views:

417

answers:

5

If I have a string such as

"17:31:51 up 134 days, 11:26, 1 user, load average: 0.22, 0.15, 0.10"

what is the best way to extract just the x3 load average values at the end? I have written a regexp that does this but is this the most efficient / fastest method?

>>> s = "17:31:51 up 134 days, 11:26,  1 user,  load average: 0.22, 0.15, 0.10"
>>> print re.findall(r"([0-9]\.\d+)", s)
['0.22', '0.15', '0.10']
+6  A: 

This should work:

# s is the string to parse
loadavg = [float(x) for x in s.rsplit('load average: ', 1)[1].split(', ')]
Adam Rosenfield
+3  A: 

You have the same information in /proc/loadavg special file, so you can do:

>>> open("/proc/loadavg").readline().split(" ")[:3]
Jaime Soriano
...depending on your OS
Dustin
Couldn't be uptime output also different?
Jaime Soriano
A: 

Your way seems fine. If you want to avoid regexps you could do something like

>>> print s.split(': ')[1].split(', ')
['0.22', '0.15', '0.10']
dF
A: 

I'd use a regex, definitely. You could perhaps increase efficiency a bit by first calling s.find('load average') and starting the regexp match from that position instead of at the beginning of the string (which is the default).

David Zaslavsky
Actually, I like Adam's way better.
David Zaslavsky
A: 

A regular expression is the way. But maybe more robustly:

re.search(r"load average: (\d+.\d\d), (\d+.\d\d), (\d+.\d\d)$", s).groups()

Unless you're doing this really often in a tight loop or some such you needn't worry about performance. Clarity is what's most important. And there I'd say this regexp is hard to beat.

PEZ
A clear regex? Are you kidding?
Benjamin Peterson
Think about it as a format string. Clear as clear can be, no?
PEZ