1

So I have a CSV document that has metadata, an XPATH and a Regex String in each row. The script uses the xpath to iterate over API requests and then I want to use the regex stored in the CSV with that xpath to search for something in the API results. My issue is how to use the data in a CSV row as a literal regex search string, like r'^\w{2}.+' versus a string to search against.

with open(rules, 'r+') as rulefile:
    rreader = csv.DictReader(rulefile)
    for row in rreader:
        for ip, apikey in keydict.iteritems():
            rulequery = {'type': 'config', 'action': 'get', 'key': apikey, 'xpath': row["xpath"]}
            rrule = requests.get('https://' + ip + '/api', params = rulequery, verify=False)
            rrex = re.compile('{}'.format(row["regex"]), re.MULTILINE)
            for line in rrule.text:
                config = rrex.findall(line)
                print(config)
2
  • What is the issue ? It doesn't work ? Do you get an error ? Commented May 11, 2015 at 18:51
  • No it just returns... [] [] [] [] [] [] [] [] [] [] Commented May 11, 2015 at 19:18

1 Answer 1

1

So I think I may have found a solution, although Im not sure it is the best... Open for assistance if anyone has a better way to do it.

with open(rules, 'r+') as rulefile:
rreader = csv.DictReader(rulefile)
for row in rreader:
    for ip, apikey in keydict.iteritems():
        regex = row["regex"]
        rulequery = {'type': 'config', 'action': 'get', 'key': apikey, 'xpath': row["xpath"]}
        rrule = requests.get('https://' + ip + '/api', params = rulequery, verify=False)
        config = re.search(regex, rrule.text)
        print rrule.text[config.start():config.end()]
Sign up to request clarification or add additional context in comments.

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.