2

I have a list of elements. Each element in the list has a string value in the following format:

Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(87)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]

I would like to extract only the numbers. The numbers always comes in brackets after Name[wi, It is always the same format.

How can i extract the numbers and store it in a variable so i can use it?

My Python code will iterate over the elements and if it finds a number above 86 it should return false, else return true.

My current Python code is:

def is_match_audit_code_displayed(self):
elements = self.driver.find_elements_by_xpath('//table[@id="reporting_view_report_dg_main_body"]//tr//td[4]//span')
for i in elements:
    if "86" not in i.text:
        print i.text
        return True
    return False

Thanks, Riaz

Using alecxe answer I have included it in my Python method. In the list of elements find the numeric value inside the brackets using regex. For each item in the list check if the number value is less than 86. If it is return true else return false.

Here is the full method routine:

def is_match_audit_code_less_than_max_value_displayed_for_the_filter_report_results(self, max_code): 
    try:
        elements = self.driver.find_elements_by_xpath('//table[@id="reporting_view_report_dg_main_body"]//tr//td[4]//span')
        pattern = re.compile(r"Name\[wi ,\((\d+)\)\]")

        for element in elements:

            value = pattern.findall(element.text)
            if len(value) != 1:
                return False
            value = int(value[0])
            if value > max_code: # e.g. max_code is 86
                return False
            return True
    except NoSuchElementException, e:
        print value
        print "Element not found "
        print e
        screenshot_name = elements + value + get_datetime_now()
        self.save_screenshot(screenshot_name)

2 Answers 2

2

You can use regular expressions:

import re

elements = self.driver.find_elements_by_xpath('//table[@id="reporting_view_report_dg_main_body"]//tr//td[4]//span')
pattern = re.compile(r"Name\[wi ,\((\d+)\)\]")

for element in elements:
    print(pattern.findall(element.text))

In the Name\[wi ,\((\d+)\)\] expression we have to escape the [, ], ( and ) since these characters have a special meaning in regular expressions. The (\d+) part is a capturing group that would extract one or more digits.

Sign up to request clarification or add additional context in comments.

1 Comment

That's great, this works for me. Thanks for your help. I have posted the full method at the bottom of my question.
0

Use the following approach:

import re

string = """
Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(87)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]
"""
# look for a digit in square brackets only
# more precise: match an opening bracket, 
# anything that is not a closing bracket, 
# digits greedily (\d+),
# anything not a closing bracket lazily 
# and a closing bracket
rx = r'\[[^]]*?(\d+)[^]]*?\]'

for match in re.finditer(rx, string):
    num = match.group(1)
    print num

See a demo on regex101.com. This approach makes sure only to find the digits in square brackets (otherwise you could have simply come up with \d+).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.