为什么我的功能不迭代并将所有CSV数据写入我的XML模板
我正在编写一个代码,该代码旨在从CSV文件中获取数据并将其写入XML模板,然后用标题作为输出+记录号导出文件。这是问题,我的代码正确地创建了所有文件,并使用正确的命名,我使用了几个打印语句来调试我的代码,并且正确地通过CSV数据循环循环循环,并且计数器输出为15(我在15行中有15行我的CSV文件)因此,我发现此部分有效,我使用了元素树库来写入我的XML模板并保存输出文件一个XML文件。当我打开所有输出文件并意识到它们中的所有记录输入是CSV文件中的第一行,我不知道我的错误在
哪里我的代码:
from xml.etree import ElementTree as ET
def Task():
""" This Function uses an XML template and replaces certain variables (parameters)
with data in a csv file :)"""
CSV_File = open('records1.csv', 'r', encoding = 'UTF-8')
tree = ET.parse('Template1.xml')
root = tree.getroot()
count = 0
for line in CSV_File:
x = line.split(',')
count +=1
print(x)
for node in root.getiterator():
if node.attrib.get('val') == 'X1':
node.attrib['val'] = x[0]
if node.attrib.get('val') == 'X2':
node.attrib['val'] = x[1]
if node.attrib.get('val') == 'X3':
node.attrib['val'] = x[2]
if node.attrib.get('val') == 'X4':
node.attrib['val'] = x[3]
if node.attrib.get('val') == 'X5':
node.attrib['val'] = x[4]
if node.attrib.get('val') == 'X6':
node.attrib['val'] = x[5]
if node.attrib.get('val') == 'X7':
node.attrib['val'] = x[6]
XML_File= open('output'+str(count)+'.xml', 'wb')
tree.write(XML_File)
XML_File.close()
I am writing a code that is meant to grab data from a csv file and write it into an xml template then export the file with the title as output+record number. Here is the issue, my code properly creates all the files with the correct naming, I used a couple of print statements to debug my code and my for loop loops through my csv data properly and the counter output was 15 (i have 15 rows in my csv file) so I figured this part works, I used the Element Tree library in order to write into my xml template and to save the output file a an xml file. I got stuck when I opened all the output files and realized that all of them have the same record input in them which was the first row in the csv file, I don't know where my mistake is, can someone please help me
here is my code:
from xml.etree import ElementTree as ET
def Task():
""" This Function uses an XML template and replaces certain variables (parameters)
with data in a csv file :)"""
CSV_File = open('records1.csv', 'r', encoding = 'UTF-8')
tree = ET.parse('Template1.xml')
root = tree.getroot()
count = 0
for line in CSV_File:
x = line.split(',')
count +=1
print(x)
for node in root.getiterator():
if node.attrib.get('val') == 'X1':
node.attrib['val'] = x[0]
if node.attrib.get('val') == 'X2':
node.attrib['val'] = x[1]
if node.attrib.get('val') == 'X3':
node.attrib['val'] = x[2]
if node.attrib.get('val') == 'X4':
node.attrib['val'] = x[3]
if node.attrib.get('val') == 'X5':
node.attrib['val'] = x[4]
if node.attrib.get('val') == 'X6':
node.attrib['val'] = x[5]
if node.attrib.get('val') == 'X7':
node.attrib['val'] = x[6]
XML_File= open('output'+str(count)+'.xml', 'wb')
tree.write(XML_File)
XML_File.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生的事情是您正在编辑XML
tree
对象以保存它。通过编辑其值,它将仅在第一个循环中工作,因为之后您找不到带有
val ='x1'
的节点,但val = the_value_u_set_set_before
。。
因此,您实际上永远不会匹配第一个循环后的IF语句,而在其结尾处,您总是保存同一棵树。
只需在for循环中移动模板的解析
What's happening is that you are editing the XML
tree
object in order to save it.By editing its values it will work only on the first loop because after that you will not find a node with
val = 'X1'
but withval = the_value_u_set_before
.So you actually never match an if statement after the first loop, and at the end of it you are always saving the same tree.
Just move the parsing of the template inside the for loop