Xvs
Xvs

Reputation: 81

Unble to use strip method

Here's the input:

Vendor: SEAGATE Product: ST914602SSUN146G Revision: 0603 Serial No: 080394ENWQ Size: 146.81GB <146810536448 bytes>

My code:

        iostat_cmd = client.executeCmd('iostat -En '+disk+'|egrep \'Vendor|Size\'')
        vendor = re.search(r'Vendor:(.*)Product:',iostat_cmd)
        vendor = str(vendor.groups()).strip()
        product = re.search(r'Product:(.*)Revision:',iostat_cmd)
        product = str(product.groups()).strip()
        revision = re.search(r'Revision:(.*)Serial No:',iostat_cmd)
        revision = str(revision.groups()).strip()
        serial = re.search(r'Serial No:(.*)',iostat_cmd)
        serial = str(serial.groups()).strip()
        size = re.search(r'Size:(.*)<.*>',iostat_cmd)
        size = str(size.groups()).strip()

The problem is, it's not actually converting the result of groups() into a string, which is probably why strip() doesn't do anything. Essentially I want to remove leading and trailing whitespaces. Please do not suggest using replace because any of the match groups can be made up from multiple words (for example, the product name can be Virtual disk) and also take into account the fact that the use of whitespaces/delimiters in iostat command output is completely arbitrary, which is why I'm going through all the trouble in the first place.

Here's what happens:

jvm 2    | <type 'org.python.modules.sre.MatchObject'>
jvm 2    | <type 'str'>
jvm 2    | (u' SEAGATE  ',)
jvm 2    | (u' ST914602SSUN146G ',)
jvm 2    | (u' 0603 ',)
jvm 2    | (u' 080394EC41 \r',)
jvm 2    | (u' 146.81GB ',)
jvm 2    | (u' SEAGATE  ',)(u' ST914602SSUN146G ',)(u' 0603 ',)(u' 080394EC41 \r',)(u' 146.81GB ',)

Upvotes: 0

Views: 71

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208665

When you use .groups() on a match object you will get a tuple with the contents of any captured groups from the regex.

To get just the contents of the first capture use .group(1) instead.

Upvotes: 2

Related Questions