[Tutor] .strip question
Mitya Sirenef
msirenef at lightbird.net
Wed Jan 9 19:44:32 CET 2013
On Wed 09 Jan 2013 01:27:26 PM EST, richard kappler wrote:
> I have a sort of a dictionary resulting from psutil.disk_usage('/')
> that tells me info about my hard drive, specifically:
>
> usage(total=147491323904, used=62555189248, free=77443956736,
> percent=42.4)
>
> I'm having a bit of a brain fudge here and can't remember how to strip
> out what I want. All I want to end up with is the number percent (in
> this case 42.4) I started playing with .strip but the usage term
> before the parens gets in the way. If it weren't for that I could
> convert this into a dict and just pull the number by the key, right?
> So how do I strip out the 'usage' string? Once I do that, I think I
> know what I'm doing but here's my proposed code to look at if you would.
>
> import psutil as ps
>
> disk = ps.disk_usage('/')
>
> # whatever I need to do to strip usage out
>
> d = {}
> for item in disk.split(','):
> item = item.strip()
> key, value = item.split(':')
> key = key.strip()
> value = value.strip()
> d[key] = float(value)
> return d
>
> Mind you, this is as of yet untested code, so before you ask for
> tracebacks, I can't give any until I figure out how to get rid of the
> preceding 'usage'.
>
> regards, Richard
>
>
> --
>
> quando omni flunkus moritati
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
Is it actually a string, though? Can you do disk.percent or
disk['percent'] ?
If it IS a string, you can get percent value like so:
# split by equal sign, strip parens from last value
print(disk.split('=')[-1].strip(')'))
- mitya
--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
More information about the Tutor
mailing list