How To Write Csv Into The Next Column
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'
Solution 1:
try to change this x = (x.split('_')[0])
Solution 2:
Think of what your CSV output looks like, then think of what it SHOULD look like.
Your CSV is generated by these two loops:
forchildin 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 + ',')
forchildin root:
forchildrenin child:
forchildrensin children.findall('{http://checklists.nist.gov/xccdf/1.2}result'):
x = childrens.text
if ('pass' in x):
c = 'Completed'
else:
c = 'Ongoing'
firstFile.write('\t' + '\n' + ',' + b + ',' + c + ',' + ',' + ',' + d)
That means that anything you add to your CSV file in the second loop will be written AFTER what was added in the first loop.
I can think of two ideas to solve this from the top of my head:
- Somehow fuse the loops into one, thus generating each row in one loop iteration. I don't know if that works with your parameters, though.
- Don't add to the end of the CSV file, but add to the row you want to add by specifically telling the "write" function where to write.
Disclaimer: I'm not familiar with Python at all, this is just the logical source of the problem and two solutions that should work in theory. I do not know if either of them is practicable.
Solution 3:
This fixed it for me. I basically left it as is, put it into a CSV, then read the CSV, stored the columns as a list, removed blank spaces and exported it back out.
import os
import sys
import glob
import csv
import xml.etree.ElementTree as ET
firstFile = open("myfile.csv", "a")
path = 'C:\\Users\\JT\\Desktop\\Scripts\\Python\\xccdf\\'for fileName in glob.glob(os.path.join(path, '*.xml')):
withopen('C:\\Users\\JT\\Desktop\\Scripts\\Python\\myfile1.csv', 'w', newline='') as csvFile1:
csvWriter = csv.writer(csvFile1, delimiter=',')
# do your stuff
tree = ET.parse(fileName)
root = tree.getroot()
# Stig Titlefor child in root.findall('{http://checklists.nist.gov/xccdf/1.2}title'):
d = child.text
# hostNamefor child in root:
for children in child.findall('{http://checklists.nist.gov/xccdf/1.2}target'):
b = children.text
# V-ID 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(a + '\n')
# Statusfor child in root:
for children in child:
for childrens in children.findall('{http://checklists.nist.gov/xccdf/1.2}result'):
x = childrens.text
firstFile.write(',' + b + ',' + x + ',' + ',' + ',' + d + '\n')
withopen('C:\\Users\\JT\\Desktop\\Scripts\\Python\\myfile.csv', 'r') as csvFile:
csvReader = csv.reader(csvFile, delimiter=',')
vIDs = []
hostNames = []
status = []
stigTitles = []
for line in csvReader:
vID = line[0]
vIDs.append(vID)
try:
hostName = line[1]
hostNames.append(hostName)
except:
passtry:
state = line[2]
status.append(state)
except:
passtry:
stigTitle = line[5]
stigTitles.append(stigTitle)
except:
passwithopen('C:\\Users\\JT\\Desktop\\Scripts\\Python\\myfile1.csv', 'a', newline='') as csvFile1:
csvWriter = csv.writer(csvFile1, delimiter=',')
vIDMod = list(filter(None, vIDs))
hostNameMod = list(filter(None, hostNames))
statusMod = list(filter(None, status))
stigTitlesMod = list(filter(None, stigTitles))
csvWriter.writerows(zip(vIDMod, hostNameMod, statusMod, stigTitlesMod))
firstFile.close()
Post a Comment for "How To Write Csv Into The Next Column"