如何将CSV写入下一列

时间:2019-03-31 22:52:56

标签: python csv elementtree

我有可以写成CSV的输出。但是,由于我如何将XML设置为文本,因此输出会错误地自我迭代。我已经尝试了很多修复XML输出的方法,但是看不到任何解决方法。

我已经尝试了很多,包括修改XML语句以尝试以不同的方式写入CSV,但是由于行号的原因,我似乎无法使行与我需要的方式相匹配。对于具有不同深度的语句。

我真的不关心它是如何完成的,只要它匹配就可以了,因为数据最终会被馈送到我的SQL数据库中。

下面是我的代码,

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.text
            if ('pass' in x):
                c = 'Completed'
            else:
                c = 'Ongoing'
            firstFile.write('\t' + '\n' + ',' + b + ',' + c + ',' + ',' + ',' + d)
firstFile.close()

以下是我的CSV当前输出, enter image description here

下面是我需要的输出,

enter image description here

3 个答案:

答案 0 :(得分:0)

尝试更改此
   x =(x.split('_')[0])

答案 1 :(得分:0)

考虑一下您的CSV输出是什么样子,然后考虑它应该是什么样子。

您的CSV由以下两个循环生成:

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.text
            if ('pass' in x):
                c = 'Completed'
            else:
                c = 'Ongoing'
            firstFile.write('\t' + '\n' + ',' + b + ',' + c + ',' + ',' + ',' + d)

这意味着您在第二个循环中添加到CSV文件中的所有内容都会在第一个循环中添加后写入。

我可以想到两个想法来解决这个问题:

  1. 以某种方式将循环融合为一个,从而在一个循环迭代中生成每一行。不过,我不知道这是否适合您的参数。
  2. 不添加到CSV文件的末尾,而是通过明确告诉“写”功能在哪里写,将其添加到要添加的行中。

免责声明:我完全不熟悉Python,这只是问题的逻辑来源,并且两个解决方案在理论上应该可行。我不知道它们中的任何一个是否可行。

答案 2 :(得分:0)

这为我解决了。我基本上保持原样,将其放入CSV中,然后读取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()