python - 如何将 CSV 写入下一列
问题描述
我有可以写入 CSV 的输出。但是,由于我将 XML 设置为文本的方式,输出会错误地自行迭代。我已经尝试了很多来修复我的 XML 输出,但我没有看到任何修复它的方法。
我已经尝试了很多,包括修改我的 XML 语句以尝试以不同的方式写入 CSV,但由于 for in 语句,我似乎无法让行与我需要的方式匹配有不同的深度。
我真的不在乎它是如何完成的,只要它匹配,因为数据最终会输入到我的 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()
下面是我需要的输出,
解决方案
尝试改变这个
x = (x.split('_')[0])
推荐阅读
- docker - 如何维护 Dockerfile 中使用的秘密?
- android - Flutter,添加google-services后,给出gradle错误
- c - 使用 C 中的套接字在客户端和服务器之间发送文件和数据
- python - 我可以通过 dot net core 的本地代码运行 python 脚本,但是当我在本地机器上托管相同的代码时,它不会运行 python 脚本
- javascript - 如何在javascript中用逗号排列小胡子括号内的所有单词?
- html - 图和div标签之间绝对定位的奇怪差异
- database - 删除子表中的记录时删除父表中的记录的干净解决方案(Oracle)
- here-olp - 代理设置不适用于 HERE 验证凭据教程
- android - How to make my Smart Home app show as a suggestion in the Google Home App
- r - 如何随机抽样数据框中存在的所有列?