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)