有关re.search和indexing的python问题

时间:2018-10-17 22:58:41

标签: python

我是python和编码的新手,我试图了解re.search和索引的工作原理。

以下是我到目前为止所拥有的,我希望physical_state等于完成后的结果:(在这种情况下,它是成功的),但是我不太了解match.group(1)和re.search的工作方式。 / p>

import ops         # Import the OPS module.
import sys         # Import the sys module.
import re

# Subscription processing function
def ops_condition (o): 
    status, err_str = o.timer.relative("tag",10)
    return status

def ops_execute (o):
    handle, err_desp = o.cli.open()
    print("OPS opens the process of command:",err_desp)
    result, n11, n21 = o.cli.execute(handle,"return")
    result, n11, n21 = o.cli.execute(handle,"dis nqa results test-instance sla 1 | i Completion:")  
    match = re.search(r"Completion:", result)

    if not match:
        print("Could not determine the state.")
            return 0  # Look into what the return values mean.

    physical_state = match.group(1)  # Gets the first group from the match.
    print (physical_state)
    result = o.cli.close(handle)
    return 0

result, n11, n21 = o.cli.execute(handle,"dis nqa results test-instance sla 1 | i Completion:")

的输出
   Completion:success                   RTD OverThresholds number: 0
   Completion:success                   RTD OverThresholds number: 0
   Completion:success                   RTD OverThresholds number: 0
   Completion:success                   RTD OverThresholds number: 0
   Completion:success                   RTD OverThresholds number: 0

运行时错误

<setup>('OPS opens the process of command:', 'success')

    Oct 18 2018 06:12:57+00:00 setup %%01OPSA/3/OPS_RESULT_EXCEPTION(l)[410]:Script is test3.py, current event is tag, instance is 1381216156, exception reason is Traceback (most recent call last):
      File ".lib/frame.py", line 114, in <module>
        ret = m.ops_execute(o)
      File "flash:$_user/test3.py", line 21, in ops_execute
        physical_state = match.group(1)  # Gets the first group from the match.
    IndexError: no such group

预先感谢

0 个答案:

没有答案