# Version 4.0 import os import sys from defusedxml.ElementTree import fromstring as safe_fromstring reportfile = "" resultsfile = "" def exportSearchResultsToCSV(resultstr): resultelement = safe_fromstring(resultstr) foundEvents = "" foundReportEvents = "" for resultsChild in resultelement.getchildren(): if resultsChild.tag == "results" : if resultsChild.attrib["type"] == "reportEvents": foundReportEvents = resultsChild if resultsChild.attrib["type"] == "events": foundEvents = resultsChild rawOutput = [] reportOutput = [] if foundEvents: for segText in foundEvents.findall(".//segtext"): rawOutput.append("%s\r\n" % segText.text) f = open(resultsfile, 'w') for x in rawOutput: f.write(x) f.close() if foundReportEvents: columnList = foundReportEvents.find("cols") csvLine = "" for column in columnList.findall("col"): csvLine = "%s,%s" % (csvLine, column.text) # remove the one extra comma which will have been added in the above step csvLine = csvLine.lstrip(",") reportOutput.append("%s\r\n" % csvLine) # need the \r\n for windows. # done with that - loop through and add the data now. for oneResult in foundReportEvents.findall("result"): csvLine = "" for oneCol in oneResult.findall("td"): # empty