为什么我的功能不迭代并将所有CSV数据写入我的XML模板

发布于 2025-02-09 07:27:37 字数 1430 浏览 2 评论 0原文

我正在编写一个代码,该代码旨在从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糖 2025-02-16 07:27:37

发生的事情是您正在编辑XML tree对象以保存它。
通过编辑其值,它将仅在第一个循环中工作,因为之后您找不到带有val ='x1'的节点,但val = the_value_u_set_set_before


因此,您实际上永远不会匹配第一个循环后的IF语句,而在其结尾处,您总是保存同一棵树。
只需在for循环中移动模板的解析

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')
    count = 0
    for line in CSV_File:
      x = line.split(',')
      // moved the next 2 lines inside the loop
      tree = ET.parse('Template1.xml')
      root = tree.getroot()
      count +=1
      print(x)
      for node in root.iter():
        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()

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 with val = 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

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')
    count = 0
    for line in CSV_File:
      x = line.split(',')
      // moved the next 2 lines inside the loop
      tree = ET.parse('Template1.xml')
      root = tree.getroot()
      count +=1
      print(x)
      for node in root.iter():
        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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文