#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    180
    Rep Power
    10

    Need help with string manipulation


    I have this:

    Code:
    import commands
    info = commands.getoutput('df /var')
    print info
    I need to only print the value under Use% such as 62%.
    Could someone assist me with this please?

    Thank You,
    Laura
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hey Laura,

    Just follow the example and you should be able to see what's going on; simply I've split the commands output then only printed the part you wanted (-2). There are of course other ways to do this, this is just my preference .

    Code:
    >>> import commands
    >>> info = commands.getoutput('df /var')
    >>> info
    'Filesystem   512-blocks     Used     Avail Capacity  Mounted on\n/dev/disk0s3  116948016 14353216 102082800    12%    /'
    >>> info.split()
    ['Filesystem', '512-blocks', 'Used', 'Avail', 'Capacity', 'Mounted', 'on', '/dev/disk0s3', '116948016', '14353216', '102082800', '12%', '/']
    >>> data = info.split()
    >>> print data[-2]
    12%
    >>>
    Take care,

    Mark .
    Last edited by netytan; November 8th, 2004 at 01:01 PM.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    180
    Rep Power
    10
    Thank you so much it works perfectly.

    Laura

IMN logo majestic logo threadwatch logo seochat tools logo