Regular Expression

Steven Bethard steven.bethard at gmail.com
Wed Dec 15 02:05:52 EST 2004


Michael McGarry wrote:
> Also I am trying to parse the following string to extract the number 
> after load average.
> 
> ".... load average: 0.04, 0.02, 0.01"

In Python 2.4:

 >>> uptime='12:12:05  up 21 days, 16:31, 10 users,  load average: 0.01, 
0.02, 0.04'
 >>> _, avg_str = uptime.rsplit(':', 1)
 >>> avg_str
' 0.01, 0.02, 0.04'
 >>> avgs = [float(s) for s in avg_str.split(',')]
 >>> avgs
[0.01, 0.02, 0.040000000000000001]

I took advantage of the new str.rsplit function which splits from the 
right side instead of the left.

Steve



More information about the Python-list mailing list