如何使用多个for语句写入csv

时间:2019-03-31 11:57:27

标签: python

我有一个Python 3.7.3脚本,该脚本读取XML,解析我需要的内容,并应将结果导出到CSV。我必须对其中一个字段使用for in循环来深入了解XML树,这使其他for语句如何附加到csv上了。

运行以下命令时,我的输出文件未列出不同的V-ID(请参阅第三个root语句中的child ...语句),但是所有其他字段都是正确的。当我删除最后一个for语句并将firstFile.write语句向左移动2个选项卡时,V-ID正确显示,但是那时我没有状态,所以我知道问题出在最后一条语句中。顺便说一句,如果我将firstFile.write语句一直移到最左边,它只会在csv中返回一行,但是应该有5行。

有没有一种方法可以从输出创建一个列表,然后将它们全部合并,或者可以将firstFile.write语句向左移动两个选项卡,并将last for语句附加到特定列(本质上是将firstFile分解) .write语句)?或者您还有其他建议吗? CSV Example

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]

for child in root:
    for children in child:
        for childrens in children.findall('{http://checklists.nist.gov/xccdf/1.2}result'):
            x = childrens.text
            if ('pass' in x):
                c = 'Completed'
            else:
                c = 'Ongoing'
            firstFile.write("\n" + a + ',' + b + ',' + c + ',' + ',' + ',' + d)
firstFile.close()

1 个答案:

答案 0 :(得分:0)

最后,花了大约一周的时间解决了这个问题。我将输出转换为CSV,然后将其读回到每一列的列表中,解析空格并再次将其写出。以下是我的操作方法。

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')):
    with open('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 Title
        for child in root.findall('{http://checklists.nist.gov/xccdf/1.2}title'):
            d = child.text    
        # hostName
        for 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')
        # Status
        for 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')

with open('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:
            pass
        try:
            state = line[2]
            status.append(state)
        except:
            pass
        try:
            stigTitle = line[5]
            stigTitles.append(stigTitle)
        except:
            pass

    with open('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()