I have output that I can write into a CSV. However, because of how i setup my XML to text, the output iterates itself incorrectly. I've tried a lot to fix my XML output, but I don't see any way to fix it.
I've tried a lot, including modifying my XML statements to trying to write to CSV in different ways, but I can't seem to get the rows to match up the way I need them to be, because of the the for in statements that have different depths.
I don't really care how it's done, so long as it matches up, because the data is ultimately fed into my SQL database.
Below is my code,
import os
import sys
import glob
import xml.etree.ElementTree as ET
firstFile = open("myfile.csv", "a")
firstFile.write("V-ID,")
firstFile.write("HostName,")
firstFile.write("Status,")
firstFile.write("Comments,")
firstFile.write("Finding Details,")
firstFile.write("STIG Name,")basePath = os.path.dirname(os.path.realpath(__file__))
xmlFile = os.path.join(basePath, "C:\\Users\\myUserName\\Desktop\\Scripts\\Python\\XMLtest.xml")
tree = ET.parse(xmlFile)
root = tree.getroot()for child in root.findall('{http://checklists.nist.gov/xccdf/1.2}title'):d = child.text
for child in root:for children in child.findall('{http://checklists.nist.gov/xccdf/1.2}target'):b = children.text
for child in root.findall('{http://checklists.nist.gov/xccdf/1.2}Group'):x = (str(child.attrib))x = (x.split('_')[6])a = x[:-2]firstFile.write("\n" + a + ',')
for child in root:for children in child:for childrens in children.findall('{http://checklists.nist.gov/xccdf/1.2}result'):x = childrens.textif ('pass' in x):c = 'Completed'else:c = 'Ongoing'firstFile.write('\t' + '\n' + ',' + b + ',' + c + ',' + ',' + ',' + d)
firstFile.close()
below is my CSV current output,
below is the output I need,